:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e4e7f0;
  --text: #1b1f2e;
  --muted: #6b7286;
  --primary: #4f46e5;
  --primary-soft: #eef0ff;
  --work: #4f46e5;
  --work-soft: #eef0ff;
  --life: #0d9488;
  --life-soft: #e6f7f5;
  --warn: #d97706;
  --danger: #dc2626;
  --ok: #16a34a;
  --user-bubble: #4f46e5;
  --user-text: #ffffff;
  --shadow: 0 1px 2px rgba(20, 24, 40, .04), 0 4px 16px rgba(20, 24, 40, .05);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1117; --surface: #171a23; --surface-2: #1f2330; --border: #2a2f3e;
    --text: #e8eaf2; --muted: #9097ab; --primary: #818cf8; --primary-soft: #23264a;
    --work: #818cf8; --work-soft: #23264a; --life: #2dd4bf; --life-soft: #123532;
    --user-bubble: #4f46e5; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1117; --surface: #171a23; --surface-2: #1f2330; --border: #2a2f3e;
  --text: #e8eaf2; --muted: #9097ab; --primary: #818cf8; --primary-soft: #23264a;
  --work: #818cf8; --work-soft: #23264a; --life: #2dd4bf; --life-soft: #123532;
  --user-bubble: #4f46e5; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.6 "IBM Plex Sans Thai", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.4em; margin: 0; font-size: 14px; }

/* Buttons */
.btn-primary {
  background: var(--primary); color: #fff; border: 0; border-radius: 10px;
  padding: 11px 16px; font-weight: 600;
}
.btn-ghost {
  background: transparent; border: 1px solid var(--border); border-radius: 10px;
  padding: 6px 12px; font-size: 14px;
}
.btn-ghost:hover, .icon-btn:hover { background: var(--surface-2); }
.icon-btn {
  position: relative; width: 38px; height: 38px; border-radius: 10px;
  border: 1px solid var(--border); background: transparent; font-size: 16px;
}
.badge {
  position: absolute; top: -5px; right: -5px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; line-height: 18px;
}

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 32px 24px; display: grid; gap: 12px; text-align: center; box-shadow: var(--shadow);
}
.login-card h1 { margin: 4px 0 0; font-size: 22px; }
.login-card p { margin: 0 0 8px; }
.login-card input {
  padding: 11px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2);
}
.logo, .logo-lg {
  display: inline-grid; place-items: center; color: #fff; font-weight: 700;
  background: linear-gradient(135deg, var(--work), var(--life));
}
.logo { width: 34px; height: 34px; border-radius: 10px; }
.logo-lg { width: 56px; height: 56px; border-radius: 16px; font-size: 26px; margin: 0 auto; }

/* App shell */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
  padding-top: max(10px, env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand b { display: block; line-height: 1.2; }
.brand small { color: var(--muted); font-size: 12px; }
.top-actions { display: flex; gap: 8px; align-items: center; }
.banner {
  margin: 12px 20px 0; padding: 10px 14px; border-radius: 10px; font-size: 14px;
  background: #fff7ed; color: #9a3412; border: 1px solid #fed7aa;
}
:root[data-theme="dark"] .banner { background: #2a1d10; color: #fdba74; border-color: #5a3a17; }

.layout {
  flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(360px, 1fr);
  gap: 20px; padding: 20px; max-width: 1500px; width: 100%; margin: 0 auto;
}
.view { min-height: 0; }

/* Dashboard */
.dash { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; overflow-y: auto; align-content: start; padding-right: 4px; }
.engine { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.engine-head { display: flex; align-items: center; gap: 8px; padding: 2px 4px; }
.engine-head h2 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: .2px; }
.engine .dot { width: 10px; height: 10px; border-radius: 50%; }
.work .dot { background: var(--work); }
.life .dot { background: var(--life); }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.work .card { border-top: 3px solid var(--work); }
.life .card { border-top: 3px solid var(--life); }
.card h3 { margin: 0 0 8px; font-size: 13px; font-weight: 600; color: var(--muted); }
.list { display: flex; flex-direction: column; }
.item {
  display: flex; gap: 10px; align-items: flex-start; padding: 8px 0;
  border-top: 1px solid var(--border); font-size: 14px;
}
.item:first-child { border-top: 0; padding-top: 2px; }
.item .main { flex: 1; min-width: 0; }
.item .title { overflow-wrap: anywhere; }
.item .meta { color: var(--muted); font-size: 12.5px; }
.empty { color: var(--muted); font-size: 13.5px; padding: 4px 0; }
.check {
  flex: none; width: 18px; height: 18px; margin-top: 3px; border-radius: 6px;
  border: 2px solid var(--border); background: transparent; padding: 0;
}
.check:hover { border-color: var(--ok); background: color-mix(in srgb, var(--ok) 15%, transparent); }
.pill {
  flex: none; font-size: 11.5px; padding: 1px 8px; border-radius: 999px; margin-top: 2px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.pill.urgent, .pill.down { background: #fee2e2; color: #b91c1c; }
.pill.high, .pill.warning { background: #ffedd5; color: #c2410c; }
.pill.ok { background: #dcfce7; color: #15803d; }
.pill.maintenance { background: var(--work-soft); color: var(--work); }
:root[data-theme="dark"] .pill.urgent, :root[data-theme="dark"] .pill.down { background: #3b1414; color: #fca5a5; }
:root[data-theme="dark"] .pill.high, :root[data-theme="dark"] .pill.warning { background: #3a2410; color: #fdba74; }
:root[data-theme="dark"] .pill.ok { background: #10301c; color: #86efac; }
.overdue { color: var(--danger) !important; }
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; margin-top: 5px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--work); border-radius: 3px; }
.date-chip {
  flex: none; width: 46px; text-align: center; border-radius: 10px; padding: 3px 0;
  background: var(--life-soft); color: var(--life); font-size: 12px; line-height: 1.3; font-weight: 600;
}
.date-chip b { display: block; font-size: 16px; }
.date-chip.work-chip { background: var(--work-soft); color: var(--work); }

/* Chat */
.chat {
  display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.chat-head { display: flex; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.chat-head select {
  flex: 1; min-width: 0; padding: 6px 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2);
}
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 88%; padding: 10px 14px; border-radius: 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 6px; }
.msg.assistant { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 6px; }
.msg code { font-size: 13px; padding: 1px 5px; border-radius: 5px; background: var(--surface); }
.msg .tools { display: block; margin-top: 6px; font-size: 12px; color: var(--muted); }
.msg.typing { color: var(--muted); }
.typing span { animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }
.welcome { margin: auto; text-align: center; color: var(--muted); max-width: 320px; }
.welcome .logo-lg { margin-bottom: 10px; }
.welcome b { color: var(--text); display: block; font-size: 17px; }
.suggestions { display: flex; gap: 6px; padding: 0 12px 8px; overflow-x: auto; scrollbar-width: none; }
.suggestions button {
  flex: none; border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 5px 12px; font-size: 13px;
}
.suggestions button:hover { border-color: var(--primary); color: var(--primary); }
.composer { display: flex; gap: 8px; padding: 10px 12px 12px; border-top: 1px solid var(--border); }
.composer textarea {
  flex: 1; resize: none; max-height: 140px; padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface-2); outline: none;
}
.composer textarea:focus { border-color: var(--primary); }
.send { width: 44px; border: 0; border-radius: 12px; background: var(--primary); color: #fff; font-size: 16px; }
.send:disabled { opacity: .5; }

/* Notifications */
.notif-panel {
  position: fixed; top: 60px; right: 16px; width: min(360px, calc(100vw - 32px)); max-height: 70vh; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0, 0, 0, .18); z-index: 20;
}
.notif-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.notif { padding: 10px 14px; border-bottom: 1px solid var(--border); font-size: 14px; white-space: pre-wrap; }
.notif small { color: var(--muted); display: block; }

/* Mobile */
.bottom-nav { display: none; }
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 12px 16px; gap: 12px; }
  .view { display: none; }
  .view.active { display: flex; }
  .dash.active { display: grid; }
  .dash { grid-template-columns: 1fr; }
  .topbar { padding-left: 16px; padding-right: 16px; }
  .hide-sm { display: none; }
  .banner { margin: 10px 16px 0; }
  .bottom-nav {
    display: flex; border-top: 1px solid var(--border); background: var(--surface);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .bottom-nav button {
    flex: 1; border: 0; background: none; padding: 8px 0 6px; display: flex; flex-direction: column;
    align-items: center; font-size: 18px; color: var(--muted);
  }
  .bottom-nav button span { font-size: 12px; }
  .bottom-nav button.active { color: var(--primary); }
}
