:root {
  --bg: #FAF7F0;
  --surface: #FFFFFF;
  --surface-alt: #F4EFE3;
  --border: #E5DEC9;
  --ink: #241914;
  --muted: #6E6259;
  --espresso: #3D1A0B;
  --navy: #1E3557;
  --navy-hover: #152640;
  --gold: #E8C547;
  --gold-deep: #B88B1C;
  --gold-soft: #FBF4D9;
  --danger: #B42318;
  --danger-bg: #FEF3F2;
  --success: #166534;
  --radius: 14px;
  --shadow: 0 4px 20px rgba(36, 25, 20, 0.06);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: 'Noto Sans Georgian', 'Sylfaen', sans-serif;
  background: var(--bg); color: var(--ink);
  line-height: 1.65;
}
h1, h2, h3, h4, .brand-title {
  font-family: 'Noto Serif Georgian', 'Sylfaen', serif;
  color: var(--espresso);
  margin-top: 0;
}

.app-shell { display: flex; height: 100vh; overflow: hidden; }

/* Sidebar */
.sidebar {
  width: 280px; flex-shrink: 0;
  background: #1B293C; color: #F5F2EB;
  display: flex; flex-direction: column;
  border-right: 1px solid rgba(255,255,255,0.08);
  transition: transform 0.25s ease;
}
.brand {
  display: flex; align-items: center; gap: 12px;
  padding: 20px 18px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}
.brand-cover {
  width: 44px; height: 62px; object-fit: cover;
  border-radius: 5px; box-shadow: 0 3px 10px rgba(0,0,0,0.35);
}
.brand-title { font-size: 16px; font-weight: 700; color: var(--gold); line-height: 1.25; }
.brand-sub { font-size: 11.5px; color: #B8C4D4; margin-top: 3px; }

.nav-list { padding: 14px 10px; flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex; align-items: center; gap: 11px;
  width: 100%; padding: 11px 14px; border: none; border-radius: 10px;
  background: transparent; color: #D8E0EA;
  font-family: inherit; font-size: 13.5px; font-weight: 500;
  text-align: left; cursor: pointer; transition: all 0.15s ease;
}
.nav-item:hover { background: rgba(255,255,255,0.08); color: #FFF; }
.nav-item.active { background: var(--gold); color: var(--espresso); font-weight: 700; }
.nav-icon { font-size: 16px; width: 22px; text-align: center; }

.sidebar-footer { padding: 14px; border-top: 1px solid rgba(255,255,255,0.1); }
.sidebar-footer .btn-outline { color: #F5F2EB; border-color: rgba(255,255,255,0.28); font-size: 12.5px; text-decoration: none; text-align: center; display: block; }
.sidebar-footer .btn-outline:hover { background: rgba(255,255,255,0.1); }
.disclaimer-mini { font-size: 11px; color: #9FB0C4; margin-top: 10px; line-height: 1.45; }

/* Main area */
.main-area { flex: 1; display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.topbar {
  height: 64px; padding: 0 24px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  flex-shrink: 0;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 12px; }
.view-heading { font-size: 19px; margin: 0; color: var(--espresso); }
.mode-badge {
  font-size: 12px; padding: 5px 11px; border-radius: 20px;
  background: var(--gold-soft); color: var(--espresso); font-weight: 600;
  border: 1px solid #E5D28A;
}
.mode-badge.online { background: #E6F4EA; color: #166534; border-color: #A7D7B5; }

.content { flex: 1; overflow-y: auto; padding: 24px; }
.view { display: none; max-width: 1080px; margin: 0 auto; }
.view.active { display: block; }
#view-chat.active { display: flex; flex-direction: column; height: calc(100vh - 112px); }

/* Cards & Controls */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 22px 24px;
  box-shadow: var(--shadow); margin-bottom: 20px;
}
.card-head { margin-bottom: 18px; }
.card-head p, .muted { color: var(--muted); font-size: 13.5px; margin: 4px 0 0; }
.card-head-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }

.btn {
  border: none; border-radius: 9px; padding: 10px 18px;
  font-family: inherit; font-size: 13.5px; font-weight: 600;
  cursor: pointer; transition: all 0.15s ease;
}
.btn-sm { padding: 7px 13px; font-size: 12.5px; }
.btn-block { width: 100%; }
.btn-primary { background: var(--navy); color: #FFF; }
.btn-primary:hover { background: var(--navy-hover); }
.btn-gold { background: var(--gold); color: var(--espresso); }
.btn-gold:hover { background: #DFB834; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--ink); }
.btn-outline:hover { background: var(--surface-alt); }
.icon-btn { background: transparent; border: none; font-size: 20px; cursor: pointer; color: var(--ink); padding: 4px 8px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; font-weight: 600; color: var(--espresso); }
.field input[type="text"], .field input[type="password"], .field input[type="number"], .field select, .field textarea {
  width: 100%; padding: 10px 13px; border: 1px solid #D6CEB8;
  border-radius: 9px; font-family: inherit; font-size: 14px;
  background: #FFFEFA; color: var(--ink);
}
.field input:focus, .field textarea:focus, .field select:focus { outline: 2px solid var(--gold); border-color: var(--gold-deep); }
.field-row { display: flex; gap: 16px; flex-wrap: wrap; }
.field-row > .field { flex: 1; min-width: 220px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.three-col { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 14px; }

/* Chat */
.chat-container {
  flex: 1; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.chat-messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.msg { display: flex; gap: 12px; max-width: 88%; }
.msg-user { align-self: flex-end; flex-direction: row-reverse; }
.msg-avatar {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 13px;
  background: var(--gold); color: var(--espresso);
}
.msg-user .msg-avatar { background: var(--navy); color: #FFF; }
.msg-body {
  padding: 13px 16px; border-radius: 14px; font-size: 14.5px;
  background: var(--surface-alt); border: 1px solid var(--border);
}
.msg-user .msg-body { background: var(--navy); color: #FFF; border-color: var(--navy); }
.msg-text p { margin: 0 0 8px; }
.msg-text p:last-child { margin-bottom: 0; }
.msg-text blockquote { margin: 8px 0; padding: 8px 12px; border-left: 3px solid var(--gold-deep); background: rgba(255,255,255,0.65); border-radius: 6px; font-size: 13.5px; }
.msg-sources { margin-top: 10px; padding-top: 8px; border-top: 1px dashed #D5CCB4; display: flex; flex-wrap: wrap; gap: 6px; }
.source-pill {
  font-size: 11.5px; padding: 3px 9px; border-radius: 12px;
  background: #FFF; border: 1px solid #D5CCB4; color: var(--navy);
  cursor: pointer; font-weight: 600;
}
.source-pill:hover { background: var(--gold-soft); }

.starter-chips { padding: 8px 16px; display: flex; flex-wrap: wrap; gap: 8px; border-top: 1px solid var(--border); background: #FCFBF7; }
.chip {
  font-size: 12px; padding: 6px 12px; border-radius: 18px;
  border: 1px solid #DCD3BC; background: #FFF; color: var(--espresso);
  cursor: pointer; font-family: inherit;
}
.chip:hover { background: var(--gold-soft); border-color: var(--gold-deep); }

.chat-form {
  display: flex; gap: 10px; padding: 14px 16px;
  border-top: 1px solid var(--border); background: var(--surface);
}
.chat-form textarea {
  flex: 1; resize: none; padding: 10px 14px; border-radius: 10px;
  border: 1px solid #D6CEB8; font-family: inherit; font-size: 14px;
}

/* Distortion checkboxes & cards */
.distortion-checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 8px; }
.check-pill {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 11px; border: 1px solid var(--border); border-radius: 8px;
  background: #FFFEFA; font-size: 12.5px; cursor: pointer;
}
.check-pill input { accent-color: var(--navy); }

.journal-list { display: flex; flex-direction: column; gap: 12px; }
.journal-item {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 14px; background: #FFFEFA;
}
.journal-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.journal-cols { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; font-size: 13.5px; }
.tag { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 10px; background: var(--gold-soft); color: var(--espresso); margin: 2px; }

/* Scales (BDC & DAS) */
.scale-cat { font-weight: 700; color: var(--navy); margin: 18px 0 8px; padding-bottom: 4px; border-bottom: 2px solid var(--gold); font-size: 14.5px; }
.scale-row {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 9px 10px; border-bottom: 1px solid #EFEAE0; font-size: 13.5px;
}
.scale-row:hover { background: #FDFBF4; }
.scale-opts { display: flex; gap: 6px; flex-shrink: 0; }
.scale-opt {
  width: 34px; height: 30px; border-radius: 7px; border: 1px solid #D6CEB8;
  background: #FFF; font-size: 12.5px; font-weight: 600; cursor: pointer;
}
.scale-opt.selected { background: var(--navy); color: #FFF; border-color: var(--navy); }

.sticky-score-bar {
  position: sticky; bottom: 0; margin-top: 16px;
  background: var(--espresso); color: #FFF;
  padding: 14px 18px; border-radius: 12px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.score-num { font-size: 22px; color: var(--gold); margin: 0 4px; }
.score-level { margin-left: 10px; padding: 3px 10px; border-radius: 12px; background: rgba(255,255,255,0.14); font-size: 12.5px; }
.crisis-box { margin-top: 14px; padding: 14px 16px; border-radius: 10px; background: var(--danger-bg); border: 1px solid #FECDCA; color: var(--danger); font-size: 13.5px; }

.das-bars { display: flex; flex-direction: column; gap: 14px; margin-top: 14px; }
.das-bar-row { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; background: #FFFEFA; }
.das-bar-head { display: flex; justify-content: space-between; font-weight: 700; font-size: 14px; margin-bottom: 6px; }
.das-track { height: 12px; background: #EAE4D3; border-radius: 6px; position: relative; overflow: hidden; }
.das-mid { position: absolute; left: 50%; top: 0; bottom: 0; width: 2px; background: #666; }
.das-fill { position: absolute; top: 0; bottom: 0; border-radius: 4px; }

/* Subtabs & Lab */
.subtabs { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.subtab {
  padding: 9px 15px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); font-family: inherit; font-size: 13px; font-weight: 600;
  cursor: pointer; color: var(--espresso);
}
.subtab.active { background: var(--navy); color: #FFF; border-color: var(--navy); }
.lab-panel { display: none; }
.lab-panel.active { display: block; }

.table-wrap { margin-top: 16px; overflow-x: auto; }
table.data-tbl { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.data-tbl th, table.data-tbl td { border: 1px solid var(--border); padding: 9px 12px; text-align: left; }
table.data-tbl th { background: var(--surface-alt); color: var(--espresso); }

.va-chain { display: flex; flex-direction: column; align-items: center; gap: 6px; margin-bottom: 14px; }
.va-node { width: 100%; max-width: 640px; padding: 12px 16px; border: 1px solid var(--gold-deep); border-radius: 10px; background: var(--gold-soft); font-size: 14px; }
.va-arrow { font-size: 20px; color: var(--navy); font-weight: 700; }

/* Distortions grid */
.distortions-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 14px; }
.dist-card { border: 1px solid var(--border); border-radius: 12px; padding: 16px; background: #FFFEFA; }
.dist-num { font-size: 11.5px; font-weight: 700; color: var(--gold-deep); text-transform: uppercase; }
.dist-card h4 { margin: 4px 0 8px; font-size: 15px; }
.dist-card p { font-size: 13px; color: var(--muted); margin: 0 0 8px; }
.dist-ex { font-size: 12.5px; background: var(--surface-alt); padding: 8px 10px; border-radius: 8px; border-left: 3px solid var(--gold-deep); }

/* Book Reader */
.reader-layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 18px; align-items: start; }
.reader-main { min-width: 0; overflow-x: hidden; }
.reader-sidebar { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; max-height: calc(100vh - 120px); overflow-y: auto; position: sticky; top: 0; }
.search-box { display: flex; gap: 6px; margin-bottom: 12px; }
.search-box input { flex: 1; padding: 8px 10px; border-radius: 8px; border: 1px solid #D6CEB8; font-family: inherit; font-size: 13px; }
.chapter-list { display: flex; flex-direction: column; gap: 4px; }
.chap-btn {
  text-align: left; padding: 8px 10px; border: none; border-radius: 8px;
  background: transparent; font-family: inherit; font-size: 12.5px; cursor: pointer; color: var(--ink);
}
.chap-btn:hover { background: var(--surface-alt); }
.chap-btn.active { background: var(--gold-soft); color: var(--espresso); font-weight: 700; }
.search-results { margin-bottom: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }
.sr-item { padding: 8px; border-radius: 8px; background: #FFFEFA; border: 1px solid var(--border); margin-bottom: 6px; cursor: pointer; font-size: 12px; }
.sr-item:hover { background: var(--gold-soft); }

.reader-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid var(--border); }
.badge-part { font-size: 12px; font-weight: 700; color: var(--navy); background: #E9EFF7; padding: 4px 10px; border-radius: 12px; }
.book-prose { font-family: 'Noto Serif Georgian', 'Sylfaen', serif; font-size: 15.5px; line-height: 1.8; overflow-wrap: break-word; }
.book-prose table { display: block; overflow-x: auto; max-width: 100%; border-collapse: collapse; margin: 16px 0; font-family: 'Noto Sans Georgian', sans-serif; font-size: 13px; }
.book-prose pre { overflow-x: auto; white-space: pre-wrap; background: var(--surface-alt); padding: 12px 14px; border-radius: 10px; font-family: 'Noto Sans Georgian', sans-serif; font-size: 13.5px; }
.book-prose th, .book-prose td { border: 1px solid var(--border); padding: 8px 10px; }
.book-prose th { background: var(--surface-alt); }
.book-prose blockquote { border-left: 3px solid var(--gold-deep); margin: 14px 0; padding: 8px 14px; background: var(--surface-alt); border-radius: 6px; }

/* Modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55); display: flex; align-items: center; justify-content: center; z-index: 999; padding: 16px; }
.modal { width: 100%; max-width: 480px; background: var(--surface); border-radius: 16px; padding: 22px 24px; box-shadow: 0 20px 50px rgba(0,0,0,0.25); }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.hidden { display: none !important; }
.mobile-only { display: none; }

@media (max-width: 900px) {
  .sidebar { position: fixed; top: 0; bottom: 0; left: 0; z-index: 500; transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .mobile-only { display: inline-flex; }
  .two-col, .three-col, .journal-cols, .reader-layout { grid-template-columns: 1fr; }
  .reader-sidebar { position: static; max-height: 260px; }
}
