:root {
  --nav-bg: #0f1320;
  --side-bg: #1a1f2e;
  --side-text: #c6ccdc;
  --side-hover: #252b3d;
  --blue: #3d5bf0;
  --blue-dark: #2f49cf;
  --blue-soft: #e9edff;
  --text: #151a2c;
  --muted: #6a7287;
  --line: #e4e7ee;
  --bg: #ffffff;
  --bg-soft: #f5f6fa;
  --bubble-out: #4568f4;
  --danger: #d6336c;
  --ok: #0ca678;
  --radius: 14px;
  --nav-h: 56px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); }
body { overflow: hidden; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.icon-btn { background: none; border: 0; padding: 8px; border-radius: 10px; display: inline-grid; place-items: center; color: inherit; min-width: 40px; min-height: 40px; }
.icon-btn:hover { background: rgba(120, 130, 160, .16); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* Login */
#login { min-height: 100%; display: grid; place-items: center; background: var(--nav-bg); padding: 16px; }
#login form { background: #fff; padding: 28px 24px; border-radius: 16px; width: min(380px, 100%); display: grid; gap: 14px; }
#login h1 { margin: 0 0 4px; font-size: 22px; }
#login label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
#login input { padding: 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 16px; color: var(--text); }
.btn { background: var(--blue); color: #fff; border: 0; padding: 12px 16px; border-radius: 10px; font-weight: 600; min-height: 44px; }
.btn:hover { background: var(--blue-dark); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.secondary { background: #fff; color: var(--text); border: 1px solid var(--line); }
.btn.secondary:hover { background: var(--bg-soft); }
.form-error { color: var(--danger); font-size: 14px; min-height: 18px; }

/* App frame */
#app { height: 100%; display: grid; grid-template-rows: var(--nav-h) 1fr; }
#topbar { background: var(--nav-bg); color: #fff; display: flex; align-items: center; gap: 4px; padding: 0 8px 0 12px; }
#topbar .logo { font-weight: 800; letter-spacing: .3px; margin-right: 8px; display: flex; align-items: center; gap: 8px; }
#topbar .logo i { width: 26px; height: 26px; background: #fff; border-radius: 7px 7px 7px 2px; display: inline-block; }
#topbar nav { display: flex; gap: 2px; }
#topbar nav a { color: #aab1c6; text-decoration: none; padding: 17px 12px 14px; font-weight: 600; border-bottom: 3px solid transparent; }
#topbar nav a.active { color: #fff; border-bottom-color: var(--blue); }
#topbar .spacer { flex: 1; }
.pill { font-size: 12px; background: #2b3250; color: #ffd166; padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 14px; flex: none; }
.avatar.sm { width: 28px; height: 28px; font-size: 11px; }
.avatar.lg { width: 56px; height: 56px; font-size: 20px; border-radius: 16px; }
#userbtn { border: 0; background: none; padding: 4px; border-radius: 50%; }
#menu { position: absolute; right: 8px; top: calc(var(--nav-h) - 4px); background: #fff; color: var(--text); border-radius: 12px; box-shadow: 0 8px 30px rgba(15, 19, 32, .25); min-width: 200px; padding: 6px; z-index: 50; }
#menu .who { padding: 10px 12px; color: var(--muted); font-size: 13px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
#menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 10px 12px; border-radius: 8px; }
#menu button:hover { background: var(--bg-soft); }
#banner { position: fixed; left: 50%; transform: translateX(-50%); top: calc(var(--nav-h) + 8px); background: #fff3bf; color: #664d03; padding: 8px 14px; border-radius: 999px; font-size: 14px; z-index: 60; box-shadow: 0 4px 14px rgba(0, 0, 0, .15); }
#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 20px; background: #20263a; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 70; max-width: 90vw; }

#panes { position: relative; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr); }
#panes > * { min-height: 0; min-width: 0; }
#scrim { position: fixed; inset: var(--nav-h) 0 0 0; background: rgba(15, 19, 32, .45); z-index: 30; }

/* Folders */
#folders { background: var(--side-bg); color: var(--side-text); overflow-y: auto; display: flex; flex-direction: column; position: fixed; top: var(--nav-h); bottom: 0; left: 0; width: min(280px, 85vw); transform: translateX(-102%); transition: transform .2s; z-index: 40; }
#app.folders-open #folders { transform: none; }
#folders h2 { margin: 0; padding: 18px 20px; color: #fff; font-size: 20px; }
#folders .group { padding: 8px 12px; }
#folders .group-title { font-size: 11px; letter-spacing: 1px; font-weight: 700; padding: 14px 12px 6px; color: #8b93aa; }
.nav-item { display: flex; align-items: center; gap: 12px; width: 100%; border: 0; background: none; color: inherit; text-align: left; padding: 11px 12px; border-radius: 10px; font-size: 15px; }
.nav-item:hover { background: var(--side-hover); }
.nav-item.active { background: var(--blue); color: #fff; }
.nav-item .count { margin-left: auto; background: #fff; color: var(--blue); border-radius: 999px; font-size: 12px; font-weight: 700; padding: 1px 8px; }
#folders .foot { margin-top: auto; padding: 12px; border-top: 1px solid #2a3046; font-size: 12px; color: #8b93aa; }

/* List */
#listpane { display: flex; flex-direction: column; border-right: 1px solid var(--line); background: var(--bg); }
.pane-head { display: flex; align-items: center; gap: 4px; padding: 10px 12px; min-height: 60px; }
.pane-head h2 { margin: 0; font-size: 19px; flex: 1; }
#compose-btn { background: var(--blue); color: #fff; border-radius: 10px; }
#compose-btn:hover { background: var(--blue-dark); }
.search { margin: 0 12px; display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 12px; padding: 0 12px; }
.search input { border: 0; outline: 0; flex: 1; padding: 11px 0; font-size: 16px; background: none; min-width: 0; }
.search .icon { color: var(--muted); }
.toolrow { display: flex; gap: 8px; padding: 10px 12px; }
.toolrow select { flex: 1; min-width: 0; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; font-size: 15px; }
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; background: none; border: 0; padding: 12px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--text); border-bottom-color: var(--text); }
#rows { overflow-y: auto; flex: 1; padding: 6px; -webkit-overflow-scrolling: touch; }
.row { display: flex; gap: 12px; padding: 12px; border-radius: 12px; cursor: pointer; align-items: center; width: 100%; text-align: left; background: none; border: 0; position: relative; }
.row:hover { background: var(--bg-soft); }
.row.selected { background: var(--blue); color: #fff; }
.row .dot { position: absolute; left: 1px; top: 50%; width: 8px; height: 8px; margin-top: -4px; border-radius: 50%; background: var(--blue); }
.row.selected .dot { background: #fff; }
.row .meta { min-width: 0; flex: 1; }
.row .top { display: flex; gap: 6px; align-items: baseline; }
.row .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row.unread .name, .row.unread .prev { font-weight: 700; color: inherit; }
.row .time { color: var(--muted); font-size: 13px; white-space: nowrap; margin-left: auto; }
.row.selected .time, .row.selected .prev { color: #e7ecff; }
.row .prev { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; margin-top: 2px; }
.empty { color: var(--muted); text-align: center; padding: 36px 16px; }
.load-more { margin: 10px auto; display: block; }

/* Chat */
#chatpane { display: flex; flex-direction: column; background: var(--bg); min-height: 0; }
#chatpane .placeholder { margin: auto; color: var(--muted); text-align: center; padding: 20px; }
.chat-head { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-bottom: 1px solid var(--line); min-height: 60px; }
.chat-head .who { display: flex; align-items: center; gap: 10px; min-width: 0; overflow: hidden; flex: 1; background: none; border: 0; text-align: left; padding: 2px; border-radius: 10px; }
.chat-head .who b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .who small { color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .who > span:last-child { min-width: 0; }
.chat-head select { flex: 0 1 110px; min-width: 0; max-width: 150px; padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: #fff; font-size: 14px; }
.back-btn { display: inline-grid; }
#msgs { flex: 1; overflow-y: auto; padding: 14px 12px; display: flex; flex-direction: column; gap: 2px; -webkit-overflow-scrolling: touch; }
.day { text-align: center; color: var(--muted); font-size: 13px; margin: 14px 0 8px; display: flex; align-items: center; gap: 10px; }
.day::before, .day::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.sysnote { text-align: center; color: var(--muted); font-size: 13px; margin: 10px 0; }
.sysnote small { display: block; }
.bubble-row { display: flex; align-items: flex-end; gap: 8px; max-width: 100%; }
.bubble-row.out { justify-content: flex-end; }
.bubble { max-width: min(78%, 520px); padding: 10px 14px; border-radius: 18px; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.35; }
.in .bubble { background: #fff; border: 1px solid var(--line); box-shadow: 0 1px 2px rgba(20, 30, 60, .06); }
.out .bubble { background: var(--bubble-out); color: #fff; }
.bubble.failed { background: #fff0f4; color: var(--danger); border: 1px solid var(--danger); }
.stamp { color: var(--muted); font-size: 12px; margin: 3px 4px 8px; }
.stamp.out { text-align: right; }
.stamp .bad { color: var(--danger); font-weight: 600; }
.stamp button { background: none; border: 0; color: var(--blue); font-weight: 600; padding: 0; }
.slot { width: 28px; flex: none; }
.load-earlier { align-self: center; margin-bottom: 6px; }
#typing { min-height: 20px; padding: 0 16px; font-size: 13px; color: var(--muted); }
.composer { margin: 0 10px 8px; border: 1px solid var(--line); border-radius: 16px; padding: 8px 10px; box-shadow: 0 1px 4px rgba(20, 30, 60, .06); }
.composer textarea { width: 100%; border: 0; outline: 0; resize: none; font-size: 16px; line-height: 1.35; max-height: 140px; min-height: 26px; background: none; }
.composer .bar { display: flex; align-items: center; gap: 4px; justify-content: flex-end; }
.composer .bar .hint { color: var(--muted); font-size: 12px; margin-right: auto; display: none; }
.send-btn { background: var(--blue); color: #fff; border: 0; border-radius: 10px; padding: 8px 14px; display: inline-flex; gap: 6px; align-items: center; font-weight: 600; min-height: 40px; }
.send-btn:disabled { opacity: .4; }

/* Info */
#infopane { background: var(--bg); overflow-y: auto; border-left: 1px solid var(--line); position: fixed; top: var(--nav-h); bottom: 0; right: 0; width: min(380px, 100%); transform: translateX(102%); transition: transform .2s; z-index: 40; }
#app.info-open #infopane { transform: none; }
.info-head { display: flex; gap: 14px; padding: 18px 16px 8px; align-items: center; }
.info-head h2 { margin: 0; font-size: 22px; }
.info-head small { color: var(--muted); display: block; margin-top: 2px; }
.info-actions { display: flex; gap: 8px; padding: 8px 16px 14px; }
.info-actions .btn { flex: 1; display: inline-flex; gap: 8px; justify-content: center; align-items: center; }
.info-tabs { display: flex; border-bottom: 1px solid var(--line); margin-top: 4px; }
.info-tabs button { flex: 1; background: none; border: 0; padding: 14px; font-weight: 700; font-size: 12px; letter-spacing: 1px; color: var(--muted); border-bottom: 2px solid transparent; }
.info-tabs button.active { color: var(--text); border-bottom-color: var(--text); }
.info-body { padding: 8px 16px 40px; }
.info-body h3 { font-size: 20px; margin: 18px 0 8px; }
.field { margin: 14px 0; }
.field label { display: block; font-size: 11px; letter-spacing: 1px; font-weight: 700; color: var(--muted); margin-bottom: 4px; text-transform: uppercase; }
.field input, .field textarea { width: 100%; border: 1px solid transparent; border-radius: 8px; padding: 8px; margin-left: -8px; width: calc(100% + 8px); font-size: 16px; background: none; resize: vertical; }
.field input:hover, .field textarea:hover { border-color: var(--line); }
.field input:focus, .field textarea:focus { border-color: var(--blue); outline: 0; background: #fff; }
.field .static { padding: 8px 0; color: var(--blue); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.chip { background: var(--bg-soft); border-radius: 8px; padding: 6px 10px; display: inline-flex; gap: 6px; align-items: center; font-size: 14px; }
.chip button { border: 0; background: none; color: var(--muted); padding: 0 2px; font-size: 16px; line-height: 1; }
.activity { list-style: none; padding: 0; margin: 0; }
.activity li { padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.activity small { color: var(--muted); display: block; }

/* Admin */
#adminpane { overflow-y: auto; padding: 16px; max-width: 900px; width: 100%; margin: 0 auto; }
#adminpane h2 { margin: 22px 0 10px; }
#adminpane table { width: 100%; border-collapse: collapse; font-size: 14px; }
#adminpane th, #adminpane td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
#adminpane td input[type=text], #adminpane td input[type=number], #adminpane .newuser input, #adminpane .newuser select { padding: 8px; border: 1px solid var(--line); border-radius: 8px; width: 100%; font-size: 15px; }
#adminpane .desc { color: var(--muted); font-size: 13px; }
.newuser { display: grid; gap: 8px; grid-template-columns: 1fr; margin-top: 10px; }
.sim-card { background: #fff8e1; border: 1px solid #ffe08a; border-radius: 12px; padding: 12px 14px; }
.simbox { margin-top: 10px; display: grid; gap: 8px; }
.simbox input { padding: 8px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; }

/* Panels visibility: phone shows one pane at a time */
#app[data-pane="list"] #chatpane { display: none; }
#app[data-pane="chat"] #listpane { display: none; }
#app[data-pane="admin"] #listpane, #app[data-pane="admin"] #chatpane { display: none; }
#adminpane { display: none; }
#app[data-pane="admin"] #adminpane { display: block; }

@media (min-width: 768px) {
  #panes { grid-template-columns: 340px minmax(0, 1fr); }
  #app[data-pane="list"] #chatpane, #app[data-pane="chat"] #listpane { display: flex; }
  #app[data-pane="admin"] #panes { grid-template-columns: minmax(0, 1fr); }
  .back-btn { display: none; }
  .composer .bar .hint { display: block; }
}
@media (min-width: 1024px) {
  #panes { grid-template-columns: 240px 340px minmax(0, 1fr); }
  #folders { position: static; transform: none; width: auto; z-index: auto; }
  #app[data-pane="admin"] #panes { grid-template-columns: minmax(0, 1fr); }
  #app[data-pane="admin"] #folders { display: none; }
  .folders-toggle { display: none !important; }
  #scrim.for-folders { display: none !important; }
}
@media (min-width: 1280px) {
  #panes { grid-template-columns: 240px 340px minmax(0, 1fr) 340px; }
  #infopane { position: static; transform: none; width: auto; z-index: auto; }
  #app[data-pane="admin"] #infopane { display: none; }
  .info-close, .info-toggle { display: none !important; }
  #scrim.for-info { display: none !important; }
}
@media (max-width: 520px) {
  #topbar .logo span { display: none; }
  #topbar nav a { padding-left: 8px; padding-right: 8px; }
  .pill { font-size: 11px; padding: 3px 8px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
