:root {
  --bg-canvas: #F5F1E8;
  --bg-surface: #FFFDF9;
  --bg-surface-warm: #F8F5ED;
  --bg-subtle: #ECE7DB;
  --bg-tint: #E7F1EE;
  --text-primary: #16211D;
  --text-secondary: #3F5049;
  --text-muted: #5A6B64;
  --border: #DCD4C5;
  --border-soft: #EAE4D8;
  --accent: #1B5446;
  --accent-hover: #133F34;
  --accent-ink: #FFFFFF;
  --plum: #533A7B;
  --plum-soft: #F3EFF9;
  --plum-border: #D9CEE9;
  --status-pending-bg: #FEF5DC;
  --status-pending-border: #DFC06B;
  --status-pending-text: #744A06;
  --status-scheduled-bg: #E8F2FC;
  --status-scheduled-border: #A9C9EC;
  --status-scheduled-text: #1A4E82;
  --status-completed-bg: #E8F6EE;
  --status-completed-border: #96CBA9;
  --status-completed-text: #155733;
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --shadow-card: 0 1px 2px rgba(22, 33, 29, 0.05), 0 4px 12px rgba(22, 33, 29, 0.035);
  --reading-width: 720px;
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
}

*, *::before, *::after { box-sizing: border-box; }
html { font-size: 15px; background: var(--bg-canvas); color: var(--text-primary); }
body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font-sans);
  line-height: 1.55;
  color: var(--text-primary);
  background: var(--bg-canvas);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; }
.skip-link { position: absolute; left: 12px; top: -100px; z-index: 100; padding: 10px 14px; border-radius: 8px; background: var(--accent); color: #fff; text-decoration: none; }
.skip-link:focus { top: 12px; }
button, input, textarea, a { font-family: inherit; }

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

/* Header */
.app-header {
  position: sticky; top: 0; z-index: 40;
  background: rgba(245, 241, 232, 0.94);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.header-inner {
  max-width: var(--reading-width);
  margin: 0 auto;
  padding: 8px 14px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.brand { display: inline-flex; align-items: center; gap: 9px; text-decoration: none; color: var(--text-primary); }
.brand-logo { width: 32px; height: 32px; border-radius: 8px; display: block; }
.brand-name { font-family: var(--font-serif); font-size: 1.2rem; font-weight: 700; letter-spacing: -0.01em; }
.header-actions { display: flex; align-items: center; gap: 6px; }
.icon-action-btn {
  position: relative;
  width: 36px; height: 36px;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg-surface); color: var(--text-secondary);
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; transition: background 0.15s, border-color 0.15s;
}
.icon-action-btn:hover { background: var(--bg-subtle); color: var(--text-primary); }
.icon-action-btn.is-spinning svg { animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.badge-count {
  position: absolute; top: -4px; right: -4px;
  min-width: 17px; height: 17px; padding: 0 4px;
  border-radius: 999px; background: #B8422A; color: #fff;
  font-size: 10.5px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center;
  font-variant-numeric: tabular-nums;
}
.refresh-feedback {
  max-width: var(--reading-width); margin: 0 auto; padding: 0 14px;
  font-size: 12px; color: var(--accent); min-height: 0;
}
.refresh-feedback:not(:empty) { padding-bottom: 6px; }

/* Navigation tabs */
.tab-nav {
  position: sticky; top: 53px; z-index: 35;
  background: rgba(245, 241, 232, 0.94);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border-soft);
}
.tab-nav-inner {
  max-width: var(--reading-width); margin: 0 auto;
  padding: 6px 14px;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
}
.tab-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 9px 10px; border-radius: 11px; border: 1px solid transparent;
  background: transparent; color: var(--text-secondary);
  font-size: 13.5px; font-weight: 600; cursor: pointer;
}
.tab-btn[aria-selected="true"] {
  background: var(--bg-surface);
  color: var(--accent);
  border-color: var(--border);
  box-shadow: 0 1px 3px rgba(22, 33, 29, 0.06);
}

/* Main container */
.app-main {
  flex: 1;
  width: 100%; max-width: var(--reading-width);
  margin: 0 auto; padding: 16px 14px 36px;
}
.section-header { margin-bottom: 14px; }
.section-title { font-family: var(--font-serif); font-size: 1.45rem; font-weight: 700; margin: 0 0 3px; letter-spacing: -0.015em; }
.section-title--compact { font-size: 1.15rem; margin: 0; }
.section-subtitle { margin: 0; font-size: 13.5px; color: var(--text-muted); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 14px; border-radius: 10px; border: 1px solid var(--border);
  font-size: 13.5px; font-weight: 600; cursor: pointer; text-decoration: none;
  background: var(--bg-surface); color: var(--text-primary);
}
.btn--primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn--primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn--primary:disabled { opacity: 0.45; cursor: not-allowed; }
.btn--secondary { background: var(--bg-surface); color: var(--text-primary); }
.btn--ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn--ghost:hover { background: var(--bg-subtle); }
.btn--compact { padding: 6px 11px; font-size: 12.5px; }

/* Tutor panel */
.tutor-toolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  margin-bottom: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--border-soft);
}
.tutor-toolbar-actions { display: flex; align-items: center; gap: 8px; }
.welcome-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow-card);
}
.welcome-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.welcome-logo { width: 42px; height: 42px; border-radius: 10px; }
.tutor-history-link {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 6px 10px; border-radius: 9px;
  background: var(--plum-soft); border: 1px solid var(--plum-border);
  color: var(--plum); text-decoration: none;
  font-size: 11px; font-weight: 500; letter-spacing: 0.04em; text-transform: uppercase;
}
.tutor-history-link:hover { background: #E9E0F5; }
.welcome-title { font-family: var(--font-serif); font-size: 1.45rem; font-weight: 700; margin: 0 0 4px; }
.welcome-program { margin: 0 0 12px; font-size: 12.5px; color: var(--text-muted); }

/* Composer */
.composer-box {
  background: var(--bg-surface-warm);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px;
}
.composer-attachment {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 10px; margin-bottom: 8px; border-radius: 8px;
  background: var(--bg-tint); font-size: 12.5px; color: var(--accent); font-weight: 600;
}
.composer-remove { padding: 3px 8px; font-size: 12px; }
.composer-input {
  width: 100%; border: 0; background: transparent; resize: vertical;
  font-size: 14.5px; line-height: 1.5; color: var(--text-primary); outline: none;
  min-height: 64px;
}
.composer-footer {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border-soft);
}
.composer-attachments { position: relative; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.composer-attach { width: 32px; height: 32px; padding: 0; border: 1px solid var(--border); border-radius: 8px; background: var(--bg-surface); }
.composer-choices {
  position: absolute; bottom: 38px; left: 0; z-index: 20;
  display: flex; flex-direction: column; gap: 4px; padding: 6px;
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow-card); min-width: 126px;
}
.composer-choices .btn { justify-content: flex-start; padding: 6px 10px; font-size: 12.5px; }
.simple-toggle {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; color: var(--text-secondary); cursor: pointer; user-select: none;
}
.simple-toggle input { accent-color: var(--accent); margin: 0; }
.composer-send { padding: 7px 14px; font-size: 13px; }

/* Suggested questions */
.suggested-section { margin-top: 16px; }
.suggested-heading { font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-muted); margin: 0 0 8px; }
.suggested-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.suggested-btn {
  width: 100%; text-align: left;
  padding: 11px 13px; border-radius: var(--radius-md);
  border: 1px solid var(--border); background: var(--bg-surface);
  color: var(--text-primary); font-size: 13.5px; line-height: 1.4;
  cursor: pointer; transition: border-color 0.15s, background 0.15s;
}
.suggested-btn:hover { border-color: var(--accent); background: #FAFDFB; }

/* Chat log */
.chat-log { display: flex; flex-direction: column; gap: 12px; margin-bottom: 14px; }
.chat-msg { border-radius: var(--radius-md); padding: 13px 15px; }
.chat-msg--user {
  align-self: flex-end; max-width: 88%;
  background: var(--accent); color: #fff; font-size: 14px;
}
.chat-msg--tutor {
  background: var(--bg-surface); border: 1px solid var(--border);
  box-shadow: var(--shadow-card);
}
.chat-source {
  display: inline-block; font-size: 11px; font-weight: 600;
  padding: 3px 8px; border-radius: 999px;
  background: var(--bg-tint); color: var(--accent); margin-bottom: 8px;
}
.chat-msg--tutor p { margin: 0 0 8px; font-size: 14px; line-height: 1.55; }
.chat-recap {
  margin-top: 10px; padding: 10px 12px; border-radius: 10px;
  background: var(--bg-surface-warm); border-left: 3px solid var(--accent);
  font-size: 13px; color: var(--text-secondary);
}
.chat-recap strong { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--accent); margin-bottom: 3px; }
.chat-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--border-soft); }

/* Tasks */
.progress-card {
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: 14px 16px; margin-bottom: 14px;
  box-shadow: var(--shadow-card);
}
.progress-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.progress-label { font-size: 13px; font-weight: 600; color: var(--text-secondary); }
.progress-value { font-size: 14px; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.progress-bar { height: 8px; border-radius: 999px; background: var(--bg-subtle); overflow: hidden; }
.progress-fill { height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.25s ease; }

.study-card {
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius-md); margin-bottom: 12px; overflow: hidden;
  box-shadow: var(--shadow-card);
}
.study-card-heading {
  list-style: none; cursor: pointer;
  padding: 13px 15px; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  font-weight: 700; font-size: 14.5px; color: var(--text-primary);
}
.study-card-heading::-webkit-details-marker { display: none; }
.study-card-count { font-size: 12px; font-weight: 600; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.study-card-content { padding: 0 15px 14px; border-top: 1px solid var(--border-soft); }

.task-row {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 11px 0; border-bottom: 1px solid var(--border-soft);
}
.task-row:last-child { border-bottom: 0; }
.task-check { margin-top: 3px; width: 17px; height: 17px; accent-color: var(--accent); cursor: pointer; flex: none; }
.task-body { flex: 1; min-width: 0; }
.task-title-line { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.task-code { font-size: 11.5px; font-weight: 700; color: var(--accent); background: var(--bg-tint); padding: 2px 6px; border-radius: 6px; }
.task-title { font-size: 14px; font-weight: 600; }
.task-row.is-done .task-title { text-decoration: line-through; color: var(--text-muted); }
.task-meta { margin-top: 4px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12px; color: var(--text-muted); }
.status-pill {
  font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; border: 1px solid transparent; text-transform: capitalize;
}
.status-pill--pending { background: var(--status-pending-bg); border-color: var(--status-pending-border); color: var(--status-pending-text); }
.status-pill--scheduled { background: var(--status-scheduled-bg); border-color: var(--status-scheduled-border); color: var(--status-scheduled-text); }
.status-pill--completed { background: var(--status-completed-bg); border-color: var(--status-completed-border); color: var(--status-completed-text); }
.task-open-note { font-size: 12px; font-weight: 600; color: var(--accent); background: none; border: 0; padding: 0; cursor: pointer; text-decoration: underline; }

.timeline-launch {
  width: 100%; margin-top: 6px; padding: 13px 16px;
  border-radius: var(--radius-md); border: 0;
  background: var(--accent); color: #fff;
  font-size: 14.5px; font-weight: 600;
  display: flex; align-items: center; justify-content: space-between;
  cursor: pointer;
}
.timeline-launch:hover { background: var(--accent-hover); }

/* Library */
.library-heading { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.update-tag {
  font-size: 11px; font-weight: 600; padding: 3px 8px; border-radius: 999px;
  background: var(--bg-subtle); color: var(--text-secondary);
}
.library-search {
  width: 100%; padding: 10px 13px; margin-bottom: 12px;
  border-radius: var(--radius-md); border: 1px solid var(--border);
  background: var(--bg-surface); font-size: 14px; color: var(--text-primary);
}
.library-tutor-list { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 8px; }
.saved-doubt-btn {
  width: 100%; text-align: left; padding: 10px 12px;
  border-radius: 10px; border: 1px solid var(--plum-border); background: var(--plum-soft);
  cursor: pointer; display: flex; flex-direction: column; gap: 3px;
}
.saved-doubt-q { font-size: 13.5px; font-weight: 600; color: var(--text-primary); }
.saved-doubt-meta { font-size: 11.5px; color: var(--plum); }
.lib-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid var(--border-soft);
}
.lib-row:last-child { border-bottom: 0; }
.lib-info { min-width: 0; }
.lib-title { font-size: 13.5px; font-weight: 600; }
.lib-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.study-empty { font-size: 13px; color: var(--text-muted); margin: 10px 0 0; }

/* Dialogs */
.dialog-backdrop {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(22, 33, 29, 0.45);
  display: flex; align-items: flex-end; justify-content: center; padding: 12px;
}
.dialog-sheet {
  width: 100%; max-width: 520px; max-height: 82dvh; overflow: auto;
  background: var(--bg-surface); border-radius: var(--radius-lg); padding: 18px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.22);
}
.dialog-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 12px; }
.dialog-title { font-family: var(--font-serif); font-size: 1.25rem; margin: 0 0 3px; }
.dialog-subtitle { font-size: 12.5px; color: var(--text-muted); margin: 0; }
.icon-close-btn { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg-surface-warm); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
.notif-item { padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border-soft); background: var(--bg-surface-warm); margin-bottom: 8px; }
.notif-item h3 { font-size: 13.5px; margin: 0 0 3px; }
.notif-item p { font-size: 12.5px; color: var(--text-muted); margin: 0; }

.action-dialog, .timeline-dialog, .answer-reader {
  border: 1px solid var(--border); border-radius: var(--radius-lg);
  background: var(--bg-surface); color: var(--text-primary);
  padding: 18px; width: min(92vw, 440px);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.2);
}
.action-dialog::backdrop, .timeline-dialog::backdrop, .answer-reader::backdrop { background: rgba(22, 33, 29, 0.45); }
.action-dialog h2 { font-family: var(--font-serif); font-size: 1.2rem; margin: 0 0 6px; }
.action-dialog p { font-size: 13.5px; color: var(--text-secondary); margin: 0 0 12px; }
.offline-status { font-size: 12.5px; color: var(--accent); font-weight: 600; }
.action-dialog-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

.timeline-dialog { width: min(94vw, 540px); padding: 0; max-height: 82dvh; }
.timeline-dialog-header { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.timeline-dialog-header h2 { font-family: var(--font-serif); font-size: 1.25rem; margin: 0; }
.timeline-dialog-body { padding: 14px 16px; max-height: 68dvh; overflow: auto; }
.timeline-item { display: grid; grid-template-columns: 96px 1fr; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border-soft); }
.timeline-item:last-child { border-bottom: 0; }
.timeline-date { font-size: 12px; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.timeline-title { font-size: 13.5px; font-weight: 600; margin-bottom: 2px; }
.timeline-detail { font-size: 12.5px; color: var(--text-muted); }

.answer-reader { width: min(95vw, 580px); padding: 0; max-height: 85dvh; }
.answer-reader-bar { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--bg-surface-warm); }
.answer-reader-scroll { padding: 18px; max-height: 70dvh; overflow: auto; }
.answer-reader-content h2 { font-family: var(--font-serif); font-size: 1.3rem; margin: 0 0 6px; }
.answer-meta { font-size: 12px; color: var(--accent); font-weight: 600; margin-bottom: 12px; }
.answer-copy p { font-size: 14.5px; line-height: 1.65; margin: 0 0 10px; }
