:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --panel-2: #fafbfc;
  --border: #e3e6ea;
  --text: #1b1f24;
  --muted: #6b7280;
  --accent: #e9a313;
  --on-accent: #1b1f24;
  --accent-dark: #c98f0a;
  --ok: #1f9d55;
  --warn: #b7791f;
  --err: #d64545;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 8px 24px rgba(16,24,40,.05);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116; --panel: #161a20; --panel-2: #1b2027; --border: #262c34;
    --text: #e7eaee; --muted: #9aa4b1;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 30px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
html { -webkit-text-size-adjust: 100%; overscroll-behavior-y: none; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14.5px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased; overscroll-behavior-y: none;
}
button, input, select { font-family: inherit; }
.hidden { display: none !important; }

/* top loading bar */
.progress {
  position: fixed; top: var(--safe-t); left: 0; right: 0; height: 3px; z-index: 100;
  opacity: 0; transition: opacity .22s ease; pointer-events: none;
}
.progress.active { opacity: 1; }
#progress-bar {
  height: 100%; width: 0; border-radius: 0 3px 3px 0;
  background: linear-gradient(90deg, var(--accent), #ffd166);
  box-shadow: 0 0 10px color-mix(in srgb, var(--accent) 65%, transparent);
  transition: width .2s ease;
}
.muted { color: var(--muted); }
.err-text { color: var(--err); font-size: 13px; min-height: 18px; margin: 6px 0 0; }
.mobile-only { display: none !important; }

input[type="text"], input[type="password"], input[type="search"], select {
  width: 100%; padding: 10px 12px; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; font-size: 16px; outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.primary {
  background: var(--accent); color: var(--on-accent); border: none; border-radius: 9px;
  padding: 10px 18px; font-size: 14px; font-weight: 650; cursor: pointer; transition: background .15s;
}
.primary:hover { background: var(--accent-dark); }
.primary:active { transform: translateY(1px); }
.primary:disabled { opacity: .6; cursor: default; }
.primary.secondary { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
.primary.secondary:hover { border-color: var(--accent); color: var(--accent); background: var(--panel-2); }
.link-btn { background: none; border: none; color: var(--accent); font-size: 13px; font-weight: 600; cursor: pointer; padding: 0; text-align: left; }
.link-btn:hover { color: var(--accent-dark); text-decoration: underline; }
.icon-btn {
  display: inline-grid; place-items: center; width: 42px; height: 42px; flex: 0 0 auto;
  background: none; border: none; font-size: 18px; line-height: 1; cursor: pointer;
  color: var(--muted); padding: 0; border-radius: 10px; -webkit-user-select: none; user-select: none;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn:active { background: var(--panel-2); }
.icon-btn svg { display: block; }

.brand { display: flex; align-items: center; gap: 9px; }
.brand h1 { font-size: 15px; margin: 0; font-weight: 700; letter-spacing: -.2px; }
.brand.big h1 { font-size: 20px; }
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 20%, transparent); }
.badge { font-size: 12px; color: var(--muted); background: var(--panel-2); border: 1px solid var(--border); padding: 3px 9px; border-radius: 999px; }
.badge.small { font-size: 11px; }

/* ---------- login ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: calc(24px + var(--safe-t)) 24px calc(24px + var(--safe-b)); }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px; display: flex; flex-direction: column; gap: 14px;
}
#password-login { display: flex; flex-direction: column; gap: 14px; }
#passkey-login-btn { margin-top: 2px; }

/* ---------- app shell ---------- */
.app { display: grid; grid-template-columns: 224px 344px minmax(0,1fr); height: 100vh; height: 100dvh; overflow: hidden; }

.sidebar {
  background: var(--panel); border-right: 1px solid var(--border);
  padding: calc(16px + var(--safe-t)) 14px calc(16px + var(--safe-b)) calc(14px + var(--safe-l));
  display: flex; flex-direction: column; gap: 16px; overflow-y: auto; z-index: 60;
}
.compose-btn { width: 100%; }
.folders { display: flex; flex-direction: column; gap: 2px; }
.folders button {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: none; border: none; color: var(--text); text-align: left;
  padding: 10px 12px; border-radius: 8px; font-size: 14px; cursor: pointer;
}
.folders button:hover { background: var(--panel-2); }
.folders button.active { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); font-weight: 650; }
.folders .count { font-size: 12px; background: var(--accent); color: var(--on-accent); border-radius: 999px; padding: 1px 7px; font-weight: 700; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 10px; padding-top: 12px; }

.list-col { background: var(--panel-2); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-width: 0; }
.list-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.list-head #search { flex: 1; }
.list-body { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; }
.list { position: absolute; inset: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }

/* pull-to-refresh indicator (list only) */
.ptr {
  position: absolute; left: 0; right: 0; top: 0; height: 64px;
  display: grid; place-items: center; transform: translateY(-64px); opacity: 0;
  pointer-events: none; z-index: 3; will-change: transform, opacity;
}
.ptr.snap, .list.snap { transition: transform .26s cubic-bezier(.22,1,.36,1), opacity .2s ease; }
.ptr-icon {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  color: var(--accent); background: var(--panel); border: 1px solid var(--border); box-shadow: var(--shadow);
}
.ptr.spinning .ptr-icon { animation: ptrspin .8s linear infinite; }
@keyframes ptrspin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
.list-item {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 12px 14px 12px 30px;
  border-bottom: 1px solid var(--border); cursor: pointer; position: relative;
}
.list-item:hover { background: var(--panel); }
.list-item.active { background: var(--panel); }
.list-item.unread .li-from, .list-item.unread .li-subject { font-weight: 700; }
.list-item .li-from { font-size: 13.5px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .li-date { font-size: 12px; color: var(--muted); white-space: nowrap; }
.list-item .li-subject { font-size: 13.5px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .li-snippet { grid-column: 1 / -1; font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .unread-dot { position: absolute; left: 14px; top: 18px; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.list-item .star { color: var(--accent); }
.thread-badge { display: inline-block; font-size: 11px; font-weight: 700; color: var(--muted); background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 0 6px; margin-left: 2px; vertical-align: middle; }
.empty-list { padding: 40px 16px; text-align: center; color: var(--muted); font-size: 13px; }

/* skeletons */
.skel {
  border-radius: 6px;
  background: linear-gradient(90deg, color-mix(in srgb, var(--muted) 10%, var(--panel-2)) 25%,
    color-mix(in srgb, var(--muted) 26%, var(--panel-2)) 37%, color-mix(in srgb, var(--muted) 10%, var(--panel-2)) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.35s ease-in-out infinite;
}
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }
.skel-row { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.skel-row .l1 { height: 12px; width: 42%; margin-bottom: 9px; }
.skel-row .l2 { height: 12px; width: 74%; margin-bottom: 9px; }
.skel-row .l3 { height: 10px; width: 58%; opacity: .75; }
.skel-read { padding: 22px 24px; display: flex; flex-direction: column; gap: 12px; }
.skel-read .h { height: 22px; width: 58%; }
.skel-read .m { height: 12px; width: 34%; }
.skel-read .b { height: 12px; width: 100%; }
@media (prefers-reduced-motion: reduce) {
  .skel { animation: none; }
  .ptr.spinning .ptr-icon { animation: none; }
}

.read-col { background: var(--panel); overflow-y: auto; min-width: 0; -webkit-overflow-scrolling: touch; }
.empty-state { display: grid; place-items: center; height: 100%; color: var(--muted); }
.read-head { padding: 20px 24px 14px; border-bottom: 1px solid var(--border); }
.read-top { display: flex; align-items: flex-start; gap: 4px; }
.read-subject { font-size: 19px; font-weight: 700; margin: 0 0 10px; overflow-wrap: anywhere; }
.read-meta { font-size: 13px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 14px; }
.read-meta .who { color: var(--text); font-weight: 600; }
.read-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.read-actions .act {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 13px; font-size: 13px; cursor: pointer; color: var(--text);
}
.read-actions .act:hover { border-color: var(--accent); color: var(--accent); }
.read-body { padding: 18px 24px; }
.read-body .plain { white-space: pre-wrap; word-wrap: break-word; font: 13.5px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; }
.read-iframe { width: 100%; border: none; min-height: 420px; background: #fff; }
.att-list { margin: 0 24px 18px; display: flex; flex-direction: column; gap: 6px; }
.att-list a { display: flex; gap: 8px; align-items: center; padding: 10px 12px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; color: var(--text); text-decoration: none; font-size: 13px; }
.att-list a:hover { border-color: var(--accent); }
.att-list .sz { color: var(--muted); margin-left: auto; white-space: nowrap; }
.thread-strip { padding: 10px 24px; border-bottom: 1px solid var(--border); font-size: 12.5px; color: var(--muted); display: flex; gap: 14px; flex-wrap: wrap; }

/* conversation thread in the reader */
.thread-list { border-bottom: 1px solid var(--border); background: var(--panel-2); max-height: 220px; overflow-y: auto; }
.thread-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); padding: 9px 16px 5px; }
.thread-item { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; background: none; border: none; border-top: 1px solid var(--border); padding: 9px 16px; cursor: pointer; color: var(--text); }
.thread-item:hover { background: var(--panel); }
.thread-item.active { background: color-mix(in srgb, var(--accent) 12%, transparent); box-shadow: inset 3px 0 0 var(--accent); }
.ti-dir { color: var(--muted); font-size: 12px; width: 14px; flex: 0 0 auto; }
.ti-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.ti-from { font-size: 12.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ti-snip { font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ti-date { font-size: 11px; color: var(--muted); white-space: nowrap; flex: 0 0 auto; }

.backdrop { display: none; }

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(10,12,16,.5); display: grid; place-items: center; padding: 24px; z-index: 70; }
.modal-card { width: min(720px, 100%); max-height: 92vh; max-height: 92dvh; display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.modal-card.narrow { max-width: 540px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { font-size: 15px; margin: 0; }
.modal-body { padding: 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; flex: 1 1 auto; -webkit-overflow-scrolling: touch; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; align-items: center; gap: 14px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.from-inputs { display: flex; gap: 10px; flex-wrap: wrap; }
.from-inputs #c-name { flex: 1 1 160px; }
.addr-group { flex: 2 1 300px; display: flex; align-items: center; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.addr-group:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.addr-group input { border: none; background: transparent; box-shadow: none !important; border-radius: 0; }
.addr-group .at { color: var(--muted); padding: 0 2px; }
.toggle-row { display: flex; gap: 14px; }
.status { font-size: 13px; color: var(--muted); }
.status.ok { color: var(--ok); } .status.err { color: var(--err); }

.attachments { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; border-top: 1px dashed var(--border); padding-top: 12px; }
.file-btn { display: inline-flex; padding: 8px 14px; font-size: 13px; font-weight: 600; color: var(--text); background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; cursor: pointer; }
.file-btn:hover { border-color: var(--accent); color: var(--accent); }
.hint { font-size: 12px; color: var(--muted); }
#c-file-list { list-style: none; margin: 0; padding: 0; width: 100%; display: flex; flex-direction: column; gap: 6px; }
#c-file-list li { display: flex; justify-content: space-between; align-items: center; gap: 10px; font-size: 13px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
#c-file-list .rm { background: none; border: none; color: var(--err); cursor: pointer; }

#editor-wrap .ql-toolbar.ql-snow { border-color: var(--border); background: var(--panel-2); border-radius: 8px 8px 0 0; }
#editor-wrap .ql-container.ql-snow { border-color: var(--border); border-radius: 0 0 8px 8px; font-size: 15px; }
#editor-wrap .ql-editor { min-height: 220px; }
.ql-snow .ql-stroke { stroke: var(--muted); } .ql-snow .ql-fill { fill: var(--muted); } .ql-snow .ql-picker { color: var(--muted); }

.pk-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.pk-list li { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 13px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; }
.pk-list li.muted { display: block; background: none; border: none; padding: 2px 0; }
.pk-list .rm { background: none; border: none; color: var(--err); cursor: pointer; font-size: 12px; }
.modal-inline { display: flex; align-items: center; gap: 12px; margin-top: 8px; flex-wrap: wrap; }

/* API keys */
.key-reveal { display: flex; align-items: center; gap: 8px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.key-reveal code { flex: 1; font-size: 12.5px; word-break: break-all; color: var(--text); }
.key-create { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 4px; }
.key-create #key-name { flex: 1 1 200px; }
.key-create .chk { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.key-create .chk input { width: auto; }
#newkey-banner { display: flex; flex-direction: column; gap: 4px; }

.toast { position: fixed; bottom: calc(22px + var(--safe-b)); left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 999px; font-size: 13px; box-shadow: var(--shadow); z-index: 80; max-width: calc(100% - 32px); text-align: center; }

/* ================= mobile ================= */
@media (max-width: 860px) {
  .mobile-only { display: inline-grid !important; }

  .app { display: block; position: relative; }

  .backdrop { display: block; position: fixed; inset: 0; background: rgba(8,10,14,.5); z-index: 55; opacity: 0; pointer-events: none; transition: opacity .22s ease; }
  .app.nav-open .backdrop { opacity: 1; pointer-events: auto; }

  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; width: min(84%, 330px);
    transform: translateX(-104%); transition: transform .26s cubic-bezier(.4,0,.2,1);
    box-shadow: 0 0 44px rgba(0,0,0,.4); border-right: 1px solid var(--border);
  }
  .app.nav-open .sidebar { transform: none; }

  .list-col { height: 100dvh; border-right: none; }
  .list-head {
    position: sticky; top: 0; z-index: 12; gap: 4px;
    padding: 8px 10px;
    padding-top: calc(8px + var(--safe-t));
    padding-left: calc(10px + var(--safe-l)); padding-right: calc(10px + var(--safe-r));
    background: var(--panel); box-shadow: 0 1px 0 var(--border);
  }
  .list-head #search { min-width: 0; }
  .list-item { padding: 13px 16px 13px 30px; }
  .list-item .unread-dot { left: 14px; top: 19px; }
  .list { padding-bottom: var(--safe-b); }

  .read-col { display: none; border-right: none; }
  .app.reading .list-col { display: none; }
  .app.reading .read-col { display: block; height: 100dvh; }
  .read-head {
    position: sticky; top: 0; z-index: 8; background: var(--panel);
    padding: 12px 16px 12px;
    padding-top: calc(12px + var(--safe-t));
    padding-left: calc(16px + var(--safe-l)); padding-right: calc(16px + var(--safe-r));
  }
  .read-subject { font-size: 17px; }
  .read-title-row { display: flex; align-items: center; gap: 6px; }
  .read-body { padding: 16px; }
  .att-list { margin: 0 16px 16px; }
  .read-actions .act { padding: 9px 12px; }

  .modal { padding: 0; place-items: stretch; }
  .modal-card {
    width: 100%; max-width: 100%; height: 100dvh; max-height: 100dvh; border-radius: 0; border: none;
  }
  .modal-head { padding-top: calc(14px + var(--safe-t)); }
  .modal-body { padding-bottom: calc(18px + var(--safe-b)); }
  .modal-foot { padding-bottom: calc(12px + var(--safe-b)); }
  #editor-wrap .ql-editor { min-height: 34dvh; }
}

@media (min-width: 861px) {
  .app.reading .list-col { display: flex; }
  .app.reading .read-col { display: block; }
}
