:root {
  --bg: #f4f6f5;
  --panel: #ffffff;
  --ink: #17201f;
  --muted: #5d6967;
  --line: #dfe5e3;
  --brand: #0f3d3e;
  --brand-ink: #ffffff;
  --accent: #1f7a6d;
  --bot: #ffffff;
  --user: #0f3d3e;
  --user-ink: #ffffff;
  --warn-bg: #fff6e5;
  --warn-ink: #7a4b00;
  --warn-line: #f0d29b;
  --ok-bg: #e9f6f1;
  --ok-ink: #155c45;
  --err: #b42318;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1413; --panel: #161e1d; --ink: #e6ecea; --muted: #9aa8a5; --line: #26312f;
    --brand: #1d5e5c; --accent: #4fb3a2; --bot: #182220; --user: #1d5e5c;
    --warn-bg: #2b2210; --warn-ink: #f3cf8a; --warn-line: #5a4517; --ok-bg: #12291f; --ok-ink: #8fdcbc;
    --err: #ff8a80; --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.75 "Segoe UI", "Noto Sans Arabic", "Noto Naskh Arabic", Tahoma, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.app { display: flex; flex-direction: column; height: 100dvh; max-width: 860px; margin: 0 auto; }

/* ---------------------------------------------------------------- header */
.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 2;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { font-size: 1.05rem; margin: 0; line-height: 1.3; }
.tagline { margin: 0; color: var(--muted); font-size: .82rem; line-height: 1.4; }
.logo {
  flex: none; width: 40px; height: 40px; border-radius: 12px; background: var(--brand);
  display: grid; place-items: center;
}
.logo svg { width: 22px; height: 22px; fill: none; stroke: var(--brand-ink); stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.pill {
  flex: none; font-size: .72rem; padding: 2px 10px; border-radius: 999px;
  background: var(--warn-bg); color: var(--warn-ink); border: 1px solid var(--warn-line);
}
.bar-actions { display: flex; gap: 6px; flex: none; }

/* ---------------------------------------------------------------- buttons */
.btn {
  border: 1px solid transparent; border-radius: 12px; padding: 10px 18px; cursor: pointer;
  font-weight: 600; transition: background .15s, border-color .15s, opacity .15s;
}
.btn.small { padding: 6px 12px; font-size: .85rem; font-weight: 500; }
.btn.primary { background: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.ghost:hover { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
.btn:focus-visible, .chip:focus-visible, .send:focus-visible, textarea:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

/* ---------------------------------------------------------------- login and loading */
.center { flex: 1; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 28px 24px; box-shadow: var(--shadow); display: grid; gap: 12px; text-align: center;
}
.login-card h2 { margin: 0; font-size: 1.3rem; }
.login-card p { margin: 0; color: var(--muted); }
.login-card input {
  padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); text-align: center;
}
.error { color: var(--err); margin: 4px 0 0; font-size: .9rem; }

/* ---------------------------------------------------------------- chat */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread { flex: 1; overflow-y: auto; padding: 20px 16px 8px; display: flex; flex-direction: column; gap: 14px; scroll-behavior: smooth; }

.msg { display: flex; gap: 10px; align-items: flex-end; max-width: 100%; }
.msg.user { flex-direction: row-reverse; }
.avatar {
  flex: none; width: 30px; height: 30px; border-radius: 10px; background: var(--brand); display: grid; place-items: center;
}
.avatar svg { width: 17px; height: 17px; fill: none; stroke: var(--brand-ink); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.bubble {
  background: var(--bot); border: 1px solid var(--line); border-radius: var(--radius); border-bottom-right-radius: 6px;
  padding: 10px 14px; max-width: min(620px, 86%); box-shadow: var(--shadow); overflow-wrap: anywhere;
}
.msg.user .bubble {
  white-space: pre-wrap;
  background: var(--user); color: var(--user-ink); border-color: transparent;
  border-bottom-right-radius: var(--radius); border-bottom-left-radius: 6px;
}
.bubble.wide { max-width: 100%; flex: 1; white-space: normal; }
.bubble.bare { padding: 0; border: 0; background: none; box-shadow: none; }
.bubble p { margin: 0 0 6px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ol { margin: 4px 0; padding-inline-start: 22px; }
.note {
  font-size: .82rem; color: var(--warn-ink); background: var(--warn-bg); border: 1px solid var(--warn-line);
  border-radius: 12px; padding: 8px 12px; margin-top: 8px; white-space: normal;
}
.muted { color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 2px 40px 0 0; }
.chip {
  border: 1px solid var(--accent); color: var(--accent); background: transparent; border-radius: 999px;
  padding: 6px 14px; cursor: pointer; font-size: .9rem;
}
.chip:hover { background: var(--ok-bg); }
.chip.solid { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.chip:disabled { opacity: .5; cursor: default; }

.typing { display: inline-flex; align-items: center; gap: 10px; color: var(--muted); }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

.sheet {
  background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; margin-top: 8px; font-size: .95rem;
}

.sheet h4 { margin: 10px 0 2px; font-size: .9rem; color: var(--accent); }
.sheet h4:first-child { margin-top: 0; }
.sheet ul { margin: 0; padding-inline-start: 20px; }

/* ---------------------------------------------------------------- the memo */
.memo-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.memo-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--bg);
}
.memo-head h2 { margin: 0; font-size: 1rem; }
.badge { font-size: .78rem; padding: 2px 10px; border-radius: 999px; border: 1px solid var(--line); }
.badge.warn { background: var(--warn-bg); color: var(--warn-ink); border-color: var(--warn-line); }
.badge.ok { background: var(--ok-bg); color: var(--ok-ink); border-color: transparent; }
.memo { padding: 6px 18px 14px; line-height: 1.9; }
.memo h1 { font-size: 1.2rem; margin: 14px 0 8px; }
.memo h2 { font-size: 1.05rem; margin: 18px 0 6px; color: var(--accent); }
.memo h3 { font-size: .98rem; margin: 14px 0 4px; }
.memo blockquote { margin: 10px 0; padding: 8px 12px; border-inline-start: 4px solid var(--warn-line); background: var(--warn-bg); color: var(--warn-ink); border-radius: 8px; }
.memo ul, .memo ol { padding-inline-start: 22px; }
.memo code { font-size: .85em; }
.memo-notes { padding: 0 18px 12px; font-size: .9rem; }
.memo-notes summary { cursor: pointer; color: var(--muted); }
.memo-actions { display: flex; gap: 8px; flex-wrap: wrap; padding: 12px 16px; border-top: 1px solid var(--line); }

.rate { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.rate .chip[aria-pressed="true"] { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.feedback textarea {
  width: 100%; margin-top: 8px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); padding: 8px 12px; resize: vertical;
}

/* ---------------------------------------------------------------- composer */
.composer { padding: 8px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--bg); border-top: 1px solid var(--line); }
.composer-row {
  display: flex; align-items: flex-end; gap: 8px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 20px; padding: 6px 6px 6px 8px; box-shadow: var(--shadow);
}
.composer-row:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; border: 0; background: transparent; resize: none; padding: 8px 10px; max-height: 40vh; line-height: 1.6; outline: none;
}
.composer textarea:disabled { opacity: .6; }
.send {
  flex: none; width: 42px; height: 42px; border-radius: 14px; border: 0; background: var(--brand); cursor: pointer;
  display: grid; place-items: center;
}
.send svg { width: 20px; height: 20px; fill: var(--brand-ink); transform: scaleX(-1); }
.send:disabled { opacity: .4; cursor: default; }

@media (max-width: 560px) {
  .tagline, .pill { display: none; }
  .bubble { max-width: 92%; }
  .chips { margin-inline-start: 0; margin-right: 0; }
}

/* ---------------------------------------------------------------- print: the memo only */
@media print {
  body { background: #fff; color: #000; }
  .bar, .composer, .msg:not(.memo-msg), .chips, .memo-actions, .avatar, .feedback { display: none !important; }
  .app { height: auto; }
  .thread { overflow: visible; padding: 0; }
  .memo-card { border: 0; box-shadow: none; }
}
