:root {
  --bg: #f5f5f2;
  --surface: #ffffff;
  --tint: #eef0f6;
  --text: #1d1d1f;
  --muted: #5a5a60;
  --line: #d6d6d0;
  --accent: #1f4fd8;
  --on-accent: #ffffff;
  --warn-bg: #ffe7a8;
  --warn-text: #4d3000;
  --danger: #a1261d;
  --chip-grey: #ececea; --chip-grey-t: #55555b;
  --chip-amber: #fdf0d2; --chip-amber-t: #7a4b00;
  --chip-blue: #e3ebff; --chip-blue-t: #1b3fa8;
  --chip-green: #dff3e4; --chip-green-t: #1b6b34;
  --chip-red: #fde4e1; --chip-red-t: #a1261d;
  --font: -apple-system, "SF Pro Text", system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --nav-h: 60px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161618;
    --surface: #1f1f22;
    --tint: #25272e;
    --text: #f2f2f4;
    --muted: #a9a9b1;
    --line: #3a3a40;
    --accent: #7a9cff;
    --on-accent: #0b1020;
    --warn-bg: #5b4310;
    --warn-text: #ffe9b8;
    --danger: #ff9d94;
    --chip-grey: #2b2b30; --chip-grey-t: #b8b8c0;
    --chip-amber: #3a2c0e; --chip-amber-t: #f2c46b;
    --chip-blue: #1d2c5a; --chip-blue-t: #a9c0ff;
    --chip-green: #17361f; --chip-green-t: #8fdba5;
    --chip-red: #4a1a17; --chip-red-t: #ff9d94;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  overflow-wrap: break-word;
}
h1, h2, h3 { font-weight: 600; margin: 0; line-height: 1.25; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.5rem; margin-bottom: 16px; }
h3 { font-size: 1.0625rem; margin-bottom: 8px; }
p { margin: 0 0 8px; }
code { font: 0.9em var(--mono); background: var(--tint); padding: 1px 5px; border-radius: 4px; }
ul.plain { list-style: none; margin: 0 0 8px; padding: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* controls */
button, select, input, textarea { font: inherit; color: inherit; }
button {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
button.danger { color: var(--danger); }
.field { display: block; margin-bottom: 14px; font-size: 0.9375rem; color: var(--muted); }
.field input, .field textarea, .field select {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
}
.field textarea { resize: vertical; line-height: 1.5; }
.filepick { position: relative; display: flex; align-items: center; flex-wrap: wrap; gap: 4px 12px; min-height: 44px; margin-bottom: 14px; cursor: pointer; }
.filepick input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.filepick:focus-within .btnlike { outline: 2px solid var(--accent); outline-offset: 2px; }
.btnlike { display: inline-flex; align-items: center; min-height: 44px; padding: 0 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.field.inline { display: flex; align-items: center; gap: 8px; margin: 0; }
.field.inline select { margin: 0; width: auto; min-width: 12rem; }
.check { display: inline-flex; align-items: center; gap: 10px; min-height: 44px; padding-right: 8px; cursor: pointer; }
.check input { width: 22px; height: 22px; margin: 0; accent-color: var(--accent); flex: none; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 0; }
.actions.end { justify-content: flex-end; }
.hint { color: var(--muted); font-size: 0.9375rem; }
.error { color: var(--danger); font-weight: 500; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; }

/* shell */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-box { width: 100%; max-width: 22rem; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 24px; }
.login-box h1 { margin-bottom: 20px; font-size: 1.5rem; }
.login-box button { width: 100%; }
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px max(20px, calc((100% - 72rem) / 2 + 20px)); background: var(--surface); border-bottom: 1px solid var(--line); }
.mock-strip { background: var(--warn-bg); color: var(--warn-text); text-align: center; font-weight: 600; padding: 10px 16px; }
.tabs { display: flex; gap: 4px; padding: 8px max(8px, calc((100% - 72rem) / 2 + 6px)) 0; border-bottom: 1px solid var(--line); background: var(--surface); }
.tabs a {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 14px;
  color: var(--muted); text-decoration: none; font-weight: 500;
  border-bottom: 3px solid transparent;
}
.tabs a[aria-current="page"] { color: var(--text); border-bottom-color: var(--accent); }
.count { font-size: 0.8125rem; font-variant-numeric: tabular-nums; background: var(--tint); color: var(--muted); border-radius: 999px; padding: 0 7px; min-width: 1.5em; text-align: center; }
.count:empty { display: none; }
.content { max-width: 72rem; margin: 0 auto; padding: 24px 20px 48px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 16px; margin-bottom: 16px; }
.narrow-col { max-width: 44rem; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 16px; margin: 8px 0 0; }

/* lists */
.rows { list-style: none; margin: 12px 0 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.rows:empty { display: none; }
.rows > li { border-top: 1px solid var(--line); }
.rows > li:first-child { border-top: 0; }
.row { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; grid-template-areas: "check who who" "check company chip" "check sub sub" "check acts acts"; gap: 0 8px; align-items: center; padding: 6px 12px; }
.row .check { grid-area: check; align-self: start; }
.row .who { grid-area: who; min-width: 0; padding-top: 8px; }
.row .company { grid-area: company; min-width: 0; padding-bottom: 0; margin-bottom: 6px; }
.row .chip { grid-area: chip; align-self: center; margin-top: 0; white-space: normal; max-width: 9rem; }
.row .sub { grid-area: sub; min-width: 0; }
.row .acts { grid-area: acts; display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 0 6px; }
.row .acts:empty { display: none; }
.row.nocheck { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "who who" "company chip" "sub sub" "acts acts"; }
@media (min-width: 760px) {
  .row { grid-template-columns: auto minmax(0, 1fr) auto auto; grid-template-areas: "check who chip acts" "check company company acts" "check sub sub sub"; }
  .row.nocheck { grid-template-columns: minmax(0, 1fr) auto auto; grid-template-areas: "who chip acts" "company company acts" "sub sub sub"; }
  .row .chip { white-space: nowrap; max-width: none; align-self: start; margin-top: 8px; }
  .row .acts { padding: 0; }
}
.clamp { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; }
.excerpt { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 1; line-clamp: 1; overflow: hidden; }
@media (max-width: 700px) { .excerpt { -webkit-line-clamp: 2; line-clamp: 2; } }
details.panel > summary { min-height: 44px; display: flex; align-items: center; font-weight: 600; font-size: 1.0625rem; cursor: pointer; }
details.panel[open] > summary { margin-bottom: 8px; }
.rows > li.sel { background: var(--tint); }
button.item:focus-visible { outline-offset: -3px; }
.who { font-weight: 600; overflow-wrap: anywhere; }
.company { color: var(--muted); overflow-wrap: anywhere; }
.line { font-size: 0.9375rem; overflow-wrap: anywhere; }
.line.err { color: var(--danger); }
.line.warn { color: var(--chip-amber-t); }
.line.mute { color: var(--muted); }

.chip { display: inline-flex; align-items: center; gap: 6px; font-size: 0.8125rem; font-weight: 600; padding: 2px 10px; border-radius: 999px; white-space: nowrap; background: var(--chip-grey); color: var(--chip-grey-t); }
.chip::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.chip.amber { background: var(--chip-amber); color: var(--chip-amber-t); }
.chip.blue { background: var(--chip-blue); color: var(--chip-blue-t); }
.chip.green { background: var(--chip-green); color: var(--chip-green-t); }
.chip.red { background: var(--chip-red); color: var(--chip-red-t); }

/* master / detail */
.split { display: grid; grid-template-columns: minmax(0, 22rem) minmax(0, 1fr); gap: 20px; align-items: start; }
.split .rows { margin-top: 12px; }
.back { display: none; margin-bottom: 12px; }
button.item { display: block; width: 100%; text-align: left; border: 0; border-radius: 0; background: transparent; padding: 8px 12px; min-height: 56px; }
.item-row { display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; }
.item-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.item .who, .item .company { display: block; }
.unread-dot { color: var(--accent); font-weight: 700; font-size: 0.8125rem; white-space: nowrap; }
@media (max-width: 859px) {
  .split { grid-template-columns: minmax(0, 1fr); }
  .split[data-show="list"] .detail-pane { display: none; }
  .split[data-show="detail"] .list-pane { display: none; }
  .back { display: inline-flex; align-items: center; }
}

/* letter */
.letter-wrap { max-width: 44rem; }
.letter { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 20px 22px; margin-bottom: 14px; }
.letter .meta { color: var(--muted); font-size: 0.9375rem; margin-bottom: 12px; }
.letter .meta b { color: var(--text); font-weight: 600; overflow-wrap: anywhere; }
.letter .subject { font-weight: 600; font-size: 1.0625rem; margin: 0 0 14px; padding-bottom: 12px; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
.letter-body { white-space: pre-wrap; max-width: 68ch; font-size: 1.0625rem; line-height: 1.6; overflow-wrap: anywhere; }
.ai-block { display: block; padding: 2px 0 2px 14px; border-left: 3px solid var(--accent); }
.ai-block.gap-top { margin-top: 1.6em; }
.ai-block.gap-bottom { margin-bottom: 1.6em; }
.ai-inline { border-left: 3px solid var(--accent); padding-left: 4px; background: var(--tint); }
.ai-label { display: block; font: 0.75rem var(--font); color: var(--accent); font-weight: 600; letter-spacing: 0.02em; white-space: normal; }
.facts { margin: 0 0 12px; padding: 0; list-style: none; font-size: 0.9375rem; }
.facts li { padding: 2px 0; overflow-wrap: anywhere; }
.facts .warn { color: var(--chip-amber-t); font-weight: 500; }
.facts .err { color: var(--danger); font-weight: 500; }
.facts .mute { color: var(--muted); }
.edit-fields .field { color: var(--muted); }
.edit-fields textarea { font-size: 1.0625rem; }

/* conversation */
.msg { border: 1px solid var(--line); border-radius: 14px; padding: 14px 18px; margin-bottom: 12px; background: var(--surface); }
.msg.in { background: var(--tint); }
.msg.auto .subject, .msg.auto .letter-body { color: var(--muted); }
.msg .meta { color: var(--muted); font-size: 0.875rem; margin-bottom: 6px; overflow-wrap: anywhere; }
.msg .meta b { color: var(--text); }
.msg .subject { font-weight: 600; margin-bottom: 6px; overflow-wrap: anywhere; }
.tag { display: inline-block; font-size: 0.75rem; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: var(--chip-grey); color: var(--chip-grey-t); margin-left: 6px; }
.mailbox-bar { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 12px; }
.mailbox-bar > div { flex: 1 1 16rem; min-width: 0; }

/* send */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); gap: 12px 20px; margin: 0 0 12px; }
.stats dt { color: var(--muted); font-size: 0.875rem; }
.stats dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.banner { padding: 10px 14px; border-radius: 10px; margin: 0 0 12px; background: var(--chip-red); color: var(--chip-red-t); font-weight: 500; }
.banner.info { background: var(--chip-blue); color: var(--chip-blue-t); }
.group-title { margin: 24px 0 0; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

/* notice and dialogs */
.notice {
  position: fixed; z-index: 10; left: 50%; transform: translateX(-50%);
  bottom: 20px; width: min(36rem, calc(100vw - 24px));
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--text); color: var(--bg); padding: 8px 8px 8px 16px; border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}
.notice.bad { background: var(--chip-red); color: var(--chip-red-t); border: 1px solid var(--chip-red-t); }
.notice button { background: transparent; color: inherit; border-color: currentColor; flex: none; }
dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--surface); color: var(--text); padding: 22px; width: min(30rem, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow: auto; }
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { font-size: 1.25rem; margin-bottom: 10px; }
#confirm-text { white-space: pre-line; }

/* phone */
@media (max-width: 700px) {
  body { padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); }
  .top { padding: 8px 16px; }
  .content { padding: 16px 16px 32px; }
  h2 { font-size: 1.375rem; }
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
    padding: 0 0 env(safe-area-inset-bottom); gap: 0;
    border-top: 1px solid var(--line); border-bottom: 0;
  }
  .tabs a { flex: 1 1 0; min-width: 0; flex-direction: column; gap: 0; min-height: var(--nav-h); padding: 4px 0; font-size: 0.6875rem; border-bottom: 0; border-top: 3px solid transparent; }
  .tabs a[aria-current="page"] { border-top-color: var(--accent); }
  .count { font-size: 0.6875rem; padding: 0 5px; line-height: 1.3; }
  .notice { bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom)); }
  .field.inline { flex-direction: column; align-items: stretch; width: 100%; }
  .field.inline select { width: 100%; }
  .letter { padding: 16px; }
  .actions button { flex: 1 1 auto; }
  .panel { padding: 14px; }
}
.row .check, .item-row .check { min-width: 44px; justify-content: center; padding: 0; }
.actions + .letter, .actions + .facts { margin-top: 16px; }
@media (max-width: 859px) {
  .view:has(.split[data-show="detail"]) .mailbox-bar { display: none; }
}
.letter-wrap > .toolbar { margin: 0 0 8px; }
details.panel > summary::after { content: "Rozbalit"; margin-left: auto; color: var(--muted); font-weight: 400; font-size: 0.9375rem; }
details.panel[open] > summary::after { content: "Sbalit"; }
.item .who.clamp, .item .excerpt { display: -webkit-box; }

.top-actions { display: flex; align-items: center; gap: 8px; }
.top .btnlike { text-decoration: none; color: inherit; }
.top .btnlike[aria-current="page"] { border-color: var(--accent); }
.quiet { border-color: transparent; background: transparent; color: var(--muted); text-decoration: underline; padding: 0 8px; }
.actions.tight { margin: -8px 0 14px; }
.note { min-height: 1.5em; margin: 0 0 8px; font-size: 0.9375rem; color: var(--muted); }
.note.error { color: var(--danger); font-weight: 500; }
.field .hint { display: block; margin-top: 4px; font-size: 0.875rem; }
.mode-box { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); }
.mode-text { font-weight: 600; }
@media (max-width: 700px) {
  .top { flex-wrap: wrap; }
  .top-actions button, .top .btnlike { padding: 0 12px; }
}
.note:empty { min-height: 0; margin: 0; }

/* accounts, team, ownership */
.top h1 { flex: none; }
.top-user { margin-left: auto; min-width: 0; color: var(--muted); font-size: 0.9375rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.team { margin-bottom: 24px; }
.team .rows { margin: 0 0 16px; }
.user-row { padding: 12px 14px; }
.user-row.off .who { color: var(--muted); }
.user-row .actions { margin-top: 10px; }
.chips { display: inline-flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; flex: none; }
.secret-box { background: var(--chip-amber); border: 1px solid var(--chip-amber-t); border-radius: 14px; padding: 16px; margin-bottom: 16px; }
.secret-box p:last-of-type { margin-bottom: 0; }
.secret-title { font-weight: 600; font-size: 1.0625rem; margin-bottom: 2px; }
.secret-who { color: var(--muted); font-size: 0.9375rem; overflow-wrap: anywhere; }
.secret { display: block; font-size: 1.25rem; padding: 12px 14px; margin: 10px 0 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; -webkit-user-select: all; user-select: all; overflow-wrap: anywhere; }
.item .owner { display: block; }
#template-form [readonly], #account-email { background: var(--bg); }
@media (max-width: 700px) {
  .top-actions { flex: 1 1 100%; }
  .top-actions > * { flex: 1 1 auto; justify-content: center; }
}
