*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:   #4a6fa5;
  --primary-d: #3a5a8f;
  --danger:    #e53e3e;
  --green:     #38a169;
  --bg:        #f5f6fa;
  --surface:   #ffffff;
  --border:    #e2e6ea;
  --text:      #1a202c;
  --text-dim:  #718096;
  --radius:    12px;
  --shadow:    0 2px 8px rgba(0,0,0,.08);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

#app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  color: var(--text-dim);
  font-size: 1.1rem;
}

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 48px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--border);
  color: var(--text);
  transition: background .15s, transform .1s;
  -webkit-user-select: none;
  user-select: none;
  text-decoration: none;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }

.btn-primary  { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-d); }
.btn-danger   { background: var(--danger);  color: #fff; }
.btn-full     { width: 100%; }
.btn-outline  { background: transparent; border: 1.5px solid var(--border); color: var(--text-dim); font-size: .85rem; min-height: 36px; padding: 6px 14px; }
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }

.btn-link {
  background: none;
  border: none;
  padding: 4px 8px;
  color: var(--primary);
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
}
.btn-link:hover { text-decoration: underline; }

.btn-icon {
  background: none;
  border: none;
  padding: 0 8px;
  font-size: 1.3rem;
  cursor: pointer;
  min-height: 44px;
  min-width: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  border-radius: 8px;
}
.btn-icon:hover { background: var(--border); }

/* ── Header ───────────────────────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 52px;
}
.app-header > :last-child { margin-left: auto; }

.person-badge {
  background: var(--primary);
  color: #fff;
  border-radius: 20px;
  padding: 4px 12px;
  font-size: .85rem;
  font-weight: 600;
}

/* ── Card ─────────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

label {
  font-size: .85rem;
  font-weight: 600;
  color: var(--text-dim);
  display: block;
}

input[type=password], select {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color .15s;
}
input[type=password]:focus, select:focus { border-color: var(--primary); }

.error { color: var(--danger); font-size: .9rem; }

/* ── Login ────────────────────────────────────────────────────────────────── */
.view-login {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px 20px;
  gap: 16px;
}
.view-login .logo { font-size: 3rem; }
.view-login h1 { font-size: 1.6rem; font-weight: 700; }
.view-login .subtitle { color: var(--text-dim); font-size: .9rem; text-align: center; }
.view-login .card { width: 100%; max-width: 380px; }

/* ── Person select ────────────────────────────────────────────────────────── */
.view-person {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px 20px;
  gap: 20px;
  text-align: center;
}
.view-person h2 { font-size: 1.5rem; }
.view-person .hint { color: var(--text-dim); }
.person-buttons {
  display: flex;
  gap: 16px;
  width: 100%;
  max-width: 360px;
}
.btn-person {
  flex: 1;
  flex-direction: column;
  font-size: 1.1rem;
  min-height: 100px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  border: 2px solid var(--border);
}
.btn-person:hover { border-color: var(--primary); }

/* ── Mode select ──────────────────────────────────────────────────────────── */
.view-mode { display: flex; flex-direction: column; min-height: 100dvh; }
.mode-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32px 20px;
  gap: 20px;
}
.mode-content h2 { font-size: 1.4rem; }
.mode-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 400px;
}
.btn-mode {
  width: 100%;
  font-size: 1.05rem;
  background: var(--surface);
  box-shadow: var(--shadow);
  border: 1.5px solid var(--border);
  justify-content: flex-start;
  padding-left: 20px;
}
.btn-mode:hover { border-color: var(--primary); background: #eef2ff; }
.stats-hint { color: var(--text-dim); font-size: .9rem; }

/* ── Fill mode ────────────────────────────────────────────────────────────── */
.view-fill { display: flex; flex-direction: column; min-height: 100dvh; }

.fill-progress {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px 4px;
  font-size: .82rem;
  color: var(--text-dim);
}
.section-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-right: 8px;
}
.progress-counter { flex-shrink: 0; font-weight: 600; color: var(--primary); }

.progress-bar {
  height: 3px;
  background: var(--border);
  margin: 0 16px 0;
  border-radius: 2px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 2px;
  transition: width .4s;
}

.fill-body {
  flex: 1;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
}

.question-text {
  font-size: 1.1rem;
  font-weight: 500;
  line-height: 1.5;
  color: var(--text);
}

.custom-badge {
  display: inline-block;
  font-size: .7rem;
  color: var(--primary);
  vertical-align: middle;
  margin-left: 4px;
}

.answer-textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  line-height: 1.55;
  background: var(--surface);
  color: var(--text);
  resize: none;
  outline: none;
  min-height: 120px;
  font-family: inherit;
  transition: border-color .15s;
}
.answer-textarea:focus { border-color: var(--primary); }

.save-status {
  font-size: .78rem;
  height: 16px;
  transition: color .2s;
}
.save-status.saved  { color: var(--green); }
.save-status.saving { color: var(--text-dim); }
.save-status.error  { color: var(--danger); }

.fill-nav {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  position: sticky;
  bottom: 0;
}
.btn-nav { flex: 1; }

/* ── Index overlay ────────────────────────────────────────────────────────── */
.index-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 100;
  display: flex;
  align-items: flex-end;
}
.index-panel {
  width: 100%;
  max-height: 80dvh;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.index-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  flex-shrink: 0;
}
.index-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0 16px;
}
.index-section-title {
  font-size: .78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-dim);
  padding: 12px 16px 4px;
}
.index-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 9px 16px;
  cursor: pointer;
  min-height: 44px;
  transition: background .12s;
}
.index-item:hover, .index-item.active { background: #eef2ff; }
.index-dot {
  flex-shrink: 0;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  border: 2px solid var(--border);
  margin-top: 5px;
  transition: background .15s, border-color .15s;
}
.index-item.answered .index-dot { background: var(--green); border-color: var(--green); }
.index-item.active .index-dot   { border-color: var(--primary); }
.index-q-text { font-size: .88rem; color: var(--text); line-height: 1.4; }

/* ── Review mode ──────────────────────────────────────────────────────────── */
.view-review { display: flex; flex-direction: column; min-height: 100dvh; }

.person-switch {
  display: flex;
  gap: 4px;
  background: var(--bg);
  border-radius: 8px;
  padding: 2px;
}
.btn-ps {
  background: none;
  border: none;
  border-radius: 6px;
  padding: 6px 14px;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-dim);
  min-height: 36px;
  transition: background .15s, color .15s;
}
.btn-ps.active { background: var(--surface); color: var(--primary); box-shadow: 0 1px 4px rgba(0,0,0,.1); }

.review-body {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}
.review-section {}
.review-section-title {
  font-size: .82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-dim);
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.review-item {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
}
.review-q {
  font-size: .95rem;
  font-weight: 500;
  line-height: 1.45;
  margin-bottom: 10px;
}
.review-toggle {
  background: none;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: 7px 14px;
  font-size: .85rem;
  font-weight: 600;
  color: var(--primary);
  cursor: pointer;
  min-height: 36px;
  transition: background .12s;
}
.review-toggle:hover { background: #eef2ff; }
.review-answer {
  margin-top: 10px;
  padding: 12px 14px;
  background: var(--bg);
  border-radius: 8px;
  font-size: .95rem;
  line-height: 1.55;
  white-space: pre-wrap;
  color: var(--text);
  border-left: 3px solid var(--primary);
}
.empty-hint { color: var(--text-dim); text-align: center; padding: 40px 20px; font-size: .95rem; }

/* ── Add question ─────────────────────────────────────────────────────────── */
.view-add { display: flex; flex-direction: column; min-height: 100dvh; }
.app-header span { font-weight: 600; }
.add-body { padding: 16px; display: flex; flex-direction: column; gap: 16px; flex: 1; }

.custom-list { display: flex; flex-direction: column; gap: 0; }
.custom-list h4 { font-size: .82rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--text-dim); margin-bottom: 8px; }
.custom-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface);
  border-radius: 8px;
  margin-bottom: 6px;
  box-shadow: var(--shadow);
}
.custom-q-text { font-size: .9rem; line-height: 1.4; flex: 1; }
.custom-item-actions { display: flex; gap: 4px; flex-shrink: 0; padding-top: 2px; }
.custom-item-actions .btn-link { min-height: 36px; font-size: 1rem; padding: 0 6px; }

.edit-ta { width: 100%; font-size: .9rem; min-height: 60px; flex: 1; }

/* ── Error screen ─────────────────────────────────────────────────────────── */
.error-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  gap: 16px;
  padding: 20px;
  text-align: center;
  color: var(--danger);
}

/* ── Desktop tweak ────────────────────────────────────────────────────────── */
@media (min-width: 600px) {
  #app { max-width: 600px; margin: 0 auto; }
  .index-overlay { align-items: center; }
  .index-panel { max-height: 70vh; border-radius: var(--radius); }
}
