/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #2563eb;
  --primary-light: #dbeafe;
  --primary-dark: #1d4ed8;
  --bg: #f8fafc;
  --card: #ffffff;
  --text: #1e293b;
  --text-light: #64748b;
  --border: #e2e8f0;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --info: #0891b2;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.08), 0 4px 6px -4px rgba(0,0,0,.04);
}

html { font-size: 16px; scroll-behavior: smooth; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* ===== Layout ===== */
.app { display: flex; min-height: 100vh; }

/* ===== Sidebar ===== */
.sidebar {
  width: 280px;
  background: var(--card);
  border-right: 1px solid var(--border);
  position: fixed;
  top: 0; left: 0; bottom: 0;
  overflow-y: auto;
  z-index: 100;
  transition: transform .3s ease;
}
.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0;
  background: var(--card);
  z-index: 1;
}
.sidebar-header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
}
.sidebar-header p {
  font-size: .75rem;
  color: var(--text-light);
  margin-top: 2px;
}
.sidebar-nav { padding: 12px 0; }
.nav-category {
  padding: 8px 20px 4px;
  font-size: .7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-light);
}
.nav-item {
  display: block;
  padding: 8px 20px;
  font-size: .85rem;
  color: var(--text);
  text-decoration: none;
  transition: background .15s;
  border-left: 3px solid transparent;
}
.nav-item:hover { background: var(--primary-light); }
.nav-item.active {
  background: var(--primary-light);
  border-left-color: var(--primary);
  font-weight: 600;
  color: var(--primary-dark);
}
.nav-item .badge {
  font-size: .7rem;
  color: var(--text-light);
  float: right;
}

/* Mobile sidebar toggle */
.sidebar-toggle {
  display: none;
  position: fixed;
  top: 12px; left: 12px;
  z-index: 200;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  width: 40px; height: 40px;
  font-size: 1.2rem;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
}
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 90;
}

/* ===== Main Content ===== */
.main {
  margin-left: 280px;
  flex: 1;
  padding: 32px;
  max-width: 800px;
}

/* ===== Scale Card ===== */
.scale-header {
  margin-bottom: 24px;
}
.scale-header h2 {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 4px;
}
.scale-header .subtitle {
  font-size: .9rem;
  color: var(--text-light);
}
.scale-header .desc {
  margin-top: 12px;
  padding: 12px 16px;
  background: var(--primary-light);
  border-radius: var(--radius);
  font-size: .85rem;
  line-height: 1.7;
}

/* ===== Questions ===== */
.question-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
  transition: border-color .2s;
}
.question-card.answered { border-color: var(--primary); }
.question-card.unanswered-highlight {
  border-color: var(--danger);
  box-shadow: 0 0 0 2px rgba(220,38,38,.15);
}
.question-num {
  font-size: .75rem;
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 6px;
}
.question-text {
  font-size: .95rem;
  font-weight: 500;
  margin-bottom: 12px;
  line-height: 1.6;
}

/* Radio options */
.options { display: flex; flex-direction: column; gap: 6px; }
.option-label {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: .85rem;
  transition: all .15s;
  user-select: none;
}
.option-label:hover { background: #f1f5f9; border-color: #cbd5e1; }
.option-label input[type="radio"] { display: none; }
.option-label .radio-dot {
  width: 18px; height: 18px;
  border: 2px solid #cbd5e1;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  transition: all .15s;
}
.option-label .radio-dot::after {
  content: '';
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--primary);
  transform: scale(0);
  transition: transform .15s;
}
.option-label input:checked ~ .radio-dot {
  border-color: var(--primary);
}
.option-label input:checked ~ .radio-dot::after {
  transform: scale(1);
}
.option-label input:checked ~ .option-text {
  color: var(--primary-dark);
  font-weight: 500;
}

/* Horizontal options (for short labels) */
.options.horizontal {
  flex-direction: row;
  flex-wrap: wrap;
  gap: 8px;
}
.options.horizontal .option-label {
  flex: 1;
  min-width: 80px;
  justify-content: center;
  text-align: center;
  padding: 10px 8px;
  flex-direction: column;
  gap: 4px;
}

/* ===== BDI-II style: group options ===== */
.bdi-options .option-label {
  padding: 8px 14px;
}

/* ===== Input fields (for PSQI) ===== */
.input-group {
  margin-bottom: 12px;
}
.input-group label {
  display: block;
  font-size: .85rem;
  margin-bottom: 4px;
  font-weight: 500;
}
.input-group input[type="text"],
.input-group input[type="number"],
.input-group input[type="time"],
.input-group select {
  width: 100%;
  max-width: 300px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .9rem;
  font-family: inherit;
}
.input-group input:focus,
.input-group select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 28px;
  border: none;
  border-radius: 10px;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all .15s;
  font-family: inherit;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); transform: translateY(-1px); box-shadow: var(--shadow-lg); }
.btn-secondary {
  background: #f1f5f9;
  color: var(--text);
}
.btn-secondary:hover { background: #e2e8f0; }
.btn-group { display: flex; gap: 12px; margin-top: 20px; flex-wrap: wrap; }

/* ===== Results ===== */
.result-panel {
  background: var(--card);
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  padding: 24px;
  margin-top: 24px;
  box-shadow: var(--shadow-lg);
  display: none;
}
.result-panel.visible { display: block; }
.result-score {
  text-align: center;
  margin-bottom: 16px;
}
.result-score .score-num {
  font-size: 3rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1;
}
.result-score .score-max {
  font-size: 1rem;
  color: var(--text-light);
}
.result-severity {
  text-align: center;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 16px;
}
.severity-minimal { background: #dcfce7; color: #166534; }
.severity-mild { background: #fef9c3; color: #854d0e; }
.severity-moderate { background: #fed7aa; color: #9a3412; }
.severity-severe { background: #fecaca; color: #991b1b; }
.severity-normal { background: #dcfce7; color: #166534; }

.result-detail {
  font-size: .85rem;
  line-height: 1.8;
  color: var(--text-light);
  border-top: 1px solid var(--border);
  padding-top: 16px;
}
.result-detail h4 {
  color: var(--text);
  margin-bottom: 8px;
  font-size: .9rem;
}
.result-detail table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0 16px;
  font-size: .8rem;
}
.result-detail th, .result-detail td {
  padding: 6px 10px;
  border: 1px solid var(--border);
  text-align: left;
}
.result-detail th { background: #f8fafc; font-weight: 600; }
.result-detail tr.current-row { background: var(--primary-light); font-weight: 600; }

/* Sub-scores */
.sub-scores {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin: 16px 0;
}
.sub-score-card {
  background: #f8fafc;
  border-radius: 8px;
  padding: 12px;
  text-align: center;
}
.sub-score-card .label {
  font-size: .75rem;
  color: var(--text-light);
  margin-bottom: 4px;
}
.sub-score-card .value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary);
}
.sub-score-card .severity-tag {
  font-size: .7rem;
  padding: 2px 8px;
  border-radius: 4px;
  display: inline-block;
  margin-top: 4px;
  font-weight: 600;
}

/* ===== Progress bar ===== */
.progress-bar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--card);
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 24px;
}
.progress-track {
  height: 6px;
  background: #e2e8f0;
  border-radius: 3px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 3px;
  transition: width .3s ease;
  width: 0%;
}
.progress-text {
  font-size: .75rem;
  color: var(--text-light);
  margin-top: 4px;
  text-align: right;
}

/* ===== Welcome/Home page ===== */
.home-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  margin-top: 24px;
}
.home-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  cursor: pointer;
  transition: all .15s;
  text-decoration: none;
  color: var(--text);
  display: block;
}
.home-card:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-lg);
  transform: translateY(-2px);
}
.home-card .card-category {
  font-size: .7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--primary);
  margin-bottom: 6px;
}
.home-card h3 { font-size: .95rem; margin-bottom: 4px; }
.home-card p { font-size: .78rem; color: var(--text-light); }

/* ===== Disclaimer ===== */
.disclaimer {
  margin-top: 32px;
  padding: 16px;
  background: #fef3c7;
  border: 1px solid #fbbf24;
  border-radius: var(--radius);
  font-size: .8rem;
  color: #92400e;
  line-height: 1.6;
}

/* ===== Print ===== */
@media print {
  .sidebar, .sidebar-toggle, .sidebar-overlay, .progress-bar, .btn-group { display: none !important; }
  .main { margin-left: 0; max-width: 100%; }
  .result-panel { break-inside: avoid; border: 2px solid #000; }
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .sidebar-toggle { display: flex; align-items: center; justify-content: center; }
  .sidebar-overlay.open { display: block; }
  .main { margin-left: 0; padding: 16px; padding-top: 60px; }
  .scale-header h2 { font-size: 1.25rem; }
  .options.horizontal { flex-direction: column; }
  .home-grid { grid-template-columns: 1fr; }
}
