@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap");

:root {
  --canvas: #f4f5f7;
  --surface: #ffffff;
  --border: #e3e6ec;
  --border-soft: #eceef2;
  --ink: #1b1e25;
  --ink-2: #2a2f38;
  --muted: #5b6270;
  --muted-2: #3a404c;
  --primary: #3155ed;
  --primary-hover: #2848d4;
  --primary-pressed: #1f3fbf;
  --link: #3155ed;
  --link-hover: #1f3fbf;
  --bulk-bg: #eef2ff;
  --bulk-ink: #1f3fbf;
  --bulk-border: #c9d3fb;
  --danger: #b42323;
  --star: #e8a300;
  --star-stroke: #c98c00;
  --nav-active-bg: #e8edff;
  --nav-active-fg: #3155ed;
  --radius: 10px;
  --radius-card: 14px;
  --shadow-menu: 0 12px 32px rgba(20, 24, 33, 0.14);
  --overlay: rgba(20, 24, 33, 0.35);
  --row-hover: #fafbfc;
  --row-selected-ring: #b9c6f9;
  color-scheme: light;
}

a { color: var(--link); }
a:hover { color: var(--link-hover); }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: Inter, system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color: var(--ink);
  background: var(--canvas);
  line-height: 1.5;
}
button { font-family: inherit; cursor: pointer; }
input[type="search"]::-webkit-search-cancel-button { cursor: pointer; }

.app-frame {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header */
.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand-lockup {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 200px;
}
.brand-icon {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.brand-name {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.hdr-search {
  flex: 1 1 320px;
  max-width: 640px;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 42px;
  padding: 0 14px;
  background: var(--canvas);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
}
.hdr-search input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  outline: none;
  font: inherit;
  font-size: 14px;
  color: var(--ink);
}
.hdr-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}
.icon-square {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  color: var(--muted-2);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--ink);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 14px;
  border: none;
}
.status-mini {
  font-size: 11px;
  color: var(--muted);
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-body {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 16px;
  padding: 16px;
}

/* Sidebar */
.sidebar {
  flex: 0 1 230px;
  min-width: 190px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.btn-compose {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 46px;
  border: 0;
  border-radius: var(--radius);
  background: var(--primary);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
}
.btn-compose:hover { background: var(--primary-hover); }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 40px;
  padding: 0 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted-2);
  font-size: 14px;
  font-weight: 500;
  text-align: left;
}
.nav-item svg { flex-shrink: 0; opacity: 0.85; }
.nav-item span.label { flex: 1; }
.nav-item span.count { font-size: 12px; font-weight: 600; color: var(--muted); }
.nav-item:hover { background: #fff; }
.nav-item.active {
  background: var(--nav-active-bg);
  color: var(--nav-active-fg);
  font-weight: 600;
}
.nav-item.active .count { color: var(--nav-active-fg); }

.folders-block { margin-top: 4px; }
.custom-folder-nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item.custom-folder .folder-swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.nav-item.folder-drop-target {
  background: var(--nav-active-bg);
  box-shadow: inset 0 0 0 2px var(--primary);
}
.nav-item.custom-folder .folder-actions {
  display: none;
  gap: 2px;
  margin-left: auto;
}
.nav-item.custom-folder:hover .folder-actions,
.nav-item.custom-folder:focus-within .folder-actions { display: flex; }
.folder-actions button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 2px 4px;
  font-size: 12px;
  border-radius: 4px;
}
.folder-actions button:hover { color: var(--primary); background: var(--canvas); }
.msg-row-ref[draggable="true"] { cursor: grab; }
.msg-row-ref.dragging { opacity: 0.55; }
.bulk-move-wrap { display: inline-flex; align-items: center; }
.filter-conditions { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.filter-condition-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.filter-condition-row select,
.filter-condition-row input { flex: 1 1 120px; min-width: 0; padding: 8px; border: 1px solid var(--border); border-radius: 8px; font: inherit; }
.filter-check { display: flex; align-items: center; gap: 8px; font-size: 13px; margin: 6px 0; }
.filter-rules-list { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; max-height: 240px; overflow-y: auto; }
.filter-rule-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--canvas);
  font-size: 13px;
}
.filter-rule-card.disabled { opacity: 0.65; }
.filter-rule-card .rule-head { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.filter-rule-card .rule-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

.tags-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px 6px;
}
.tags-head span {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.tags-add {
  width: 28px;
  height: 28px;
  border: 0;
  background: transparent;
  border-radius: 6px;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.label-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* Main card */
.main-card {
  flex: 999 1 640px;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: calc(100vh - 120px);
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.filter-bar.hidden { display: none; }
.filter-label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  margin-right: 2px;
  font-size: 13px;
  font-weight: 500;
}
.chip {
  height: 34px;
  padding: 0 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  border: 1px solid #dadde3;
  background: #fff;
  color: var(--muted-2);
}
.chip.on {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}
.chip-link {
  margin-left: auto;
  border: 0;
  background: transparent;
  color: var(--link);
  font-size: 13px;
  font-weight: 600;
  padding: 8px;
}
.vrule { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }
.color-filter {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  background: #fff;
  padding: 0;
}
.color-filter.on { border: 2px solid var(--ink); }
.color-filter .dot { width: 14px; height: 14px; border-radius: 50%; }

.bulk-bar-ref {
  display: none;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bulk-bg);
}
.bulk-bar-ref.show { display: flex; }
.bulk-bar-ref .selected-count {
  font-size: 14px;
  font-weight: 600;
  color: var(--bulk-ink);
  margin-right: 6px;
}
.bulk-btn {
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--bulk-border);
  background: #fff;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
}
.bulk-btn.danger { color: var(--danger); }
.bulk-cancel {
  margin-left: auto;
  border: 0;
  background: transparent;
  color: var(--link);
  font-size: 13px;
  font-weight: 600;
  padding: 8px;
}

.trash-toolbar {
  display: none;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: #fafbfc;
}
.trash-toolbar.show { display: flex; }

.card-split {
  display: flex;
  flex-wrap: wrap;
  flex: 1;
  min-height: 0;
}

.list-pane {
  flex: 1 1 420px;
  max-width: 100%;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.list-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-soft);
  background: #fafbfc;
}
.list-head input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--link);
  margin: 0;
  cursor: pointer;
}
.list-head .folder-title {
  font-size: 15px;
  font-weight: 700;
}
.list-head .folder-meta {
  font-size: 13px;
  color: var(--muted);
}
.list-head .sort-sel {
  margin-left: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
  background: #fff;
}

.msg-scroll {
  flex: 1;
  overflow-y: auto;
  max-height: calc(100vh - 280px);
}

.msg-row-ref {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 14px 14px 18px;
  border-bottom: 1px solid var(--border-soft);
  background: #fff;
}
.msg-row-ref:hover { background: var(--row-hover); }
.msg-row-ref.selected {
  background: var(--bulk-bg);
  box-shadow: inset 0 0 0 1px var(--row-selected-ring);
}
.msg-row-ref.unread .from-line { font-weight: 700; }
.msg-row-ref.unread .subject-line { font-weight: 600; }
.msg-row-ref.urgent-row { box-shadow: inset 4px 0 0 #d93636; }
.msg-col-check {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding-top: 2px;
}
.msg-col-check input { width: 18px; height: 18px; accent-color: var(--link); margin: 0; }
.star-btn {
  width: 28px;
  height: 28px;
  border: 0;
  background: transparent;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #8a919e;
}
.star-btn.on { color: var(--star); }
.msg-body-btn {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  padding: 0;
  text-align: left;
  cursor: pointer;
  color: inherit;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.from-line {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  font-size: 14px;
  color: var(--ink);
}
.from-line .from-line-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.from-line .time-line { margin-left: auto; }
.unread-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--link);
  flex: none;
}
.subject-line {
  font-size: 14px;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
}
.preview-line {
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
}
.row-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}
.tag-chip {
  display: inline-flex;
  align-items: center;
  height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.time-line {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  flex: none;
  padding-top: 2px;
}
.row-actions-mini {
  display: flex;
  flex-direction: column;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}
.msg-row-ref:hover .row-actions-mini,
.msg-row-ref:focus-within .row-actions-mini { opacity: 1; }
.icon-mini {
  width: 28px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  font-size: 12px;
  padding: 0;
  color: var(--muted-2);
}
.icon-mini.danger { color: var(--danger); }

.read-pane {
  flex: 999 1 440px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 400px;
}
.read-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-soft);
}
.tb-btn {
  height: 38px;
  padding: 0 12px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.tb-btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.tb-btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.tb-btn.primary:active { background: var(--primary-pressed); border-color: var(--primary-pressed); }
.btn-compose:active { background: var(--primary-pressed); }
.tb-btn:focus-visible,
.modal-close:focus-visible,
.bulk-btn:focus-visible,
.chip:focus-visible {
  outline: 2px solid var(--link);
  outline-offset: 2px;
}
.tb-icon {
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  color: var(--muted-2);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.tb-icon.danger { color: var(--danger); }
.tb-spacer { margin-left: auto; display: flex; gap: 4px; }

.read-article {
  padding: 24px 28px 32px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  overflow-y: auto;
  flex: 1;
  max-height: calc(100vh - 220px);
}
.read-subject {
  margin: 0;
  font-size: 22px;
  line-height: 1.3;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.sender-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--border-soft);
}
.sender-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 15px;
  flex: none;
}
.meta-dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.25rem 1rem;
  font-size: 13px;
  padding: 12px 14px;
  background: var(--canvas);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.meta-dl dt { color: var(--muted); font-weight: 600; }
.body-text {
  font-size: 15px;
  line-height: 1.65;
  color: var(--ink-2);
  white-space: pre-wrap;
}
.body-html {
  font-size: 15px;
  line-height: 1.65;
  overflow-x: auto;
}
.quick-reply label span {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted-2);
}
.quick-reply textarea {
  font: inherit;
  font-size: 14px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  resize: vertical;
  width: 100%;
  margin-top: 8px;
}

.empty-read {
  flex: 1;
  min-height: 480px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--muted);
  padding: 40px;
  text-align: center;
}
.empty-read strong { color: var(--ink); font-size: 15px; }

.list-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border-soft);
  background: #fafbfc;
}
.list-footer span { flex: 1; text-align: center; font-size: 13px; color: var(--muted); }

/* Auth */
.auth-wrap {
  max-width: 26rem;
  margin: 48px auto;
  padding: 1.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-menu);
}
.auth-wrap label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-top: 0.75rem; }
.auth-wrap input {
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
}

/* Modals & toasts */
body.modal-open { overflow: hidden; }

.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right))
    max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
}
.modal-backdrop.show { display: flex; }
#modal-confirm { z-index: 120; }
.modal {
  background: var(--surface);
  border-radius: 12px;
  max-width: 28rem;
  width: 100%;
  padding: 1.25rem;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-menu);
  max-height: min(90vh, calc(100vh - 2rem));
  overflow-y: auto;
}
.modal-settings {
  display: flex;
  flex-direction: column;
  max-width: 32rem;
  padding: 0;
  overflow: hidden;
}
.modal-settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 2;
  flex-shrink: 0;
}
.modal-settings-header h2 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.modal-close {
  width: 44px;
  height: 44px;
  min-width: 44px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  color: var(--muted-2);
  font-size: 1.75rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.modal-close:hover {
  background: var(--canvas);
  color: var(--ink);
  border-color: #dadde3;
}
.modal-close span { transform: translateY(-1px); }
.modal-settings-body {
  padding: 0 1.25rem 1.25rem;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  max-height: calc(min(90vh, 100vh - 2rem) - 4.5rem);
}
.modal-user { font-size: 13px; color: var(--muted); margin: 0.85rem 0 0; }
.modal-section { font-size: 0.95rem; margin: 1.25rem 0 0.35rem; font-weight: 600; color: var(--ink); }
.modal-hint { font-size: 12px; color: var(--muted); margin: 0.35rem 0 0; }
.modal-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.modal label { display: block; font-size: 0.82rem; font-weight: 600; color: var(--muted); margin-top: 0.65rem; }
.modal input, .modal textarea, .modal select {
  width: 100%;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  margin-top: 0.25rem;
}
.toast {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  max-width: 24rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  background: var(--ink);
  color: #fff;
  font-size: 0.88rem;
  display: none;
  z-index: 110;
  box-shadow: var(--shadow-menu);
}
.toast.err { background: var(--danger); }
.toast.ok { background: var(--primary); }
#toast-undo { bottom: 4rem; display: none; align-items: center; gap: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }

.adv-filters {
  display: none;
  flex-wrap: wrap;
  gap: 8px;
  width: 100%;
  padding-top: 8px;
}
.adv-filters.show { display: flex; }
.adv-filters input {
  flex: 1 1 140px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
}

@media (max-width: 1100px) {
  .read-pane { display: none; }
  .app-body.read-open .list-pane { display: none; }
  .app-body.read-open .read-pane { display: flex; flex: 1 1 100%; }
}
@media (max-width: 720px) {
  .sidebar { display: none; }
  .app-body.nav-open .sidebar {
    display: flex;
    position: fixed;
    left: 16px;
    top: 72px;
    z-index: 50;
    background: var(--canvas);
    max-width: 280px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-card);
    box-shadow: var(--shadow-menu);
  }
  .hdr-search { order: 3; flex: 1 1 100%; }
}