:root {
  color-scheme: light;
  --ink: #16201f;
  --muted: #65716f;
  --line: #dfe5e3;
  --soft: #f4f7f6;
  --paper: #ffffff;
  --accent: #176b61;
  --accent-hover: #10594f;
  --accent-soft: #e2f1ed;
  --gold: #b87900;
  --danger: #ad3939;
  --shadow: 0 18px 55px rgba(19, 38, 35, 0.18);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background: var(--soft);
  font-size: 14px;
}

.auth-screen {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: none;
  place-items: center;
  padding: 20px;
  background: #edf4f2;
}

.auth-pending .app-shell, .auth-required .app-shell { visibility: hidden; }
.auth-pending .auth-screen, .auth-required .auth-screen { display: grid; }
.auth-pending .auth-panel { visibility: hidden; }

.auth-panel {
  display: grid;
  width: min(360px, 100%);
  gap: 16px;
  padding: 28px;
  border: 1px solid #ccdad6;
  border-radius: 8px;
  background: white;
  box-shadow: 0 14px 42px rgba(19, 38, 35, 0.12);
}

.auth-panel img { margin-bottom: 2px; }
.auth-panel h1 { margin: 0; font-size: 24px; letter-spacing: 0; }
.auth-panel p { margin: -8px 0 4px; color: var(--muted); line-height: 1.5; }
.auth-panel label { display: grid; gap: 6px; }
.auth-panel label span { color: var(--muted); font-size: 12px; }
.auth-panel input { width: 100%; height: 42px; padding: 0 11px; border: 1px solid var(--line); border-radius: 5px; outline: 0; }
.auth-panel input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.auth-panel .send-button { width: 100%; }
.auth-link { padding: 0; border: 0; color: var(--accent); background: transparent; cursor: pointer; font-size: 12px; }
.auth-panel .auth-link { justify-self: center; }

button, input, textarea { font: inherit; }
button { color: inherit; }

.app-shell {
  display: grid;
  grid-template-columns: 220px minmax(320px, 390px) minmax(440px, 1fr);
  height: 100dvh;
  min-width: 0;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 18px 14px;
  border-right: 1px solid #ccdad6;
  background: #edf4f2;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 0 6px 20px;
}

.brand img { flex: 0 0 auto; }
.brand div { min-width: 0; }
.brand strong { display: block; font-size: 16px; letter-spacing: 0; }
.brand span {
  display: block;
  margin-top: 2px;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.compose-primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  margin-bottom: 18px;
  border: 0;
  border-radius: 6px;
  color: white;
  background: var(--accent);
  font-weight: 650;
  cursor: pointer;
}

.compose-primary:hover { background: var(--accent-hover); }
.compose-primary span { font-size: 20px; line-height: 1; }

#folders { display: grid; gap: 3px; }

.folder-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 38px;
  padding: 0 12px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  cursor: pointer;
  text-align: left;
}

.folder-button:hover { background: rgba(23, 107, 97, 0.07); }
.folder-button.active { color: var(--accent); background: var(--accent-soft); font-weight: 650; }
.folder-button b { font-size: 12px; }

.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-top: auto;
  padding: 12px 6px 0;
  color: var(--muted);
  font-size: 11px;
  overflow: hidden;
}

.sidebar-footer span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-footer .auth-link { margin-left: auto; flex: 0 0 auto; }
.status-dot { width: 7px; height: 7px; flex: 0 0 auto; border-radius: 50%; background: #35a66f; }

.mail-list-panel {
  min-width: 0;
  overflow: hidden;
  border-right: 1px solid var(--line);
  background: var(--paper);
}

.list-header {
  height: 154px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}

.title-row { display: flex; align-items: center; min-height: 36px; }
.title-row h1 { margin: 0; font-size: 20px; letter-spacing: 0; }
.title-row .icon-button { margin-left: auto; }
.mobile-menu { display: none; }

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  font-size: 19px;
  cursor: pointer;
}

.icon-button:hover { border-color: var(--line); background: var(--soft); }

.list-tools { display: grid; grid-template-columns: auto minmax(120px, 1fr); align-items: center; gap: 12px; margin-top: 8px; }
.select-page { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.select-page input, .row-check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.search-box { display: block; }
.search-box input {
  width: 100%;
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 5px;
  outline: none;
  background: var(--soft);
}

.search-box input:focus { border-color: var(--accent); background: white; box-shadow: 0 0 0 2px var(--accent-soft); }

.bulk-toolbar {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 38px;
  margin-top: 5px;
  overflow-x: auto;
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
}
.bulk-toolbar.active { visibility: visible; opacity: 1; pointer-events: auto; }
.bulk-toolbar strong { margin-right: 4px; font-size: 11px; white-space: nowrap; }
.bulk-toolbar .toolbar-button { min-height: 28px; padding-inline: 8px; font-size: 11px; white-space: nowrap; }
.bulk-toolbar .icon-button { width: 28px; height: 28px; margin-left: auto; flex: 0 0 auto; }

.message-list {
  height: calc(100dvh - 154px);
  overflow-y: auto;
  scrollbar-width: thin;
}

.message-row {
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr) auto;
  gap: 5px 10px;
  width: 100%;
  min-height: 98px;
  padding: 14px 16px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: white;
  cursor: pointer;
  text-align: left;
}

.message-row:hover { background: #f8faf9; }
.message-row:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.message-row.selected { background: var(--accent-soft); box-shadow: inset 3px 0 var(--accent); }
.message-row.checked { background: #eef7f4; }
.message-row.unread .row-sender, .message-row.unread .row-subject { font-weight: 700; }
.row-check { grid-row: 1 / span 4; align-self: start; display: grid; place-items: center; padding-top: 1px; }
.row-sender, .row-subject, .row-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sender { grid-column: 2; font-size: 13px; }
.row-time { color: var(--muted); font-size: 11px; white-space: nowrap; }
.row-subject { grid-column: 2 / -1; font-size: 13px; }
.row-preview { grid-column: 2 / -1; color: var(--muted); font-size: 12px; }
.row-flags { grid-column: 2 / -1; color: var(--gold); font-size: 12px; min-height: 14px; }

.list-state { display: grid; place-items: center; min-height: 180px; padding: 28px; color: var(--muted); text-align: center; }
.load-more { display: block; margin: -52px auto 14px; position: relative; z-index: 2; }

.reader-panel { min-width: 0; overflow: hidden; background: #fbfcfc; }
.empty-reader { display: grid; place-content: center; justify-items: center; height: 100%; color: var(--muted); }
.empty-reader img { opacity: 0.55; }
.message-reader { display: grid; grid-template-rows: auto auto minmax(0, 1fr); height: 100%; }

.reader-header { padding: 18px 28px 16px; border-bottom: 1px solid var(--line); background: white; }
.reader-toolbar { display: flex; align-items: center; gap: 6px; min-height: 36px; }
.reader-toolbar .star-action { margin-left: auto; color: var(--gold); }
.reader-toolbar .star-action.active { background: #fff4d6; }
.toolbar-button, .secondary-button, .attach-button, .field-toggle, .load-more {
  min-height: 34px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: white;
  cursor: pointer;
}
.toolbar-button:hover, .secondary-button:hover, .attach-button:hover, .load-more:hover { background: var(--soft); }
.toolbar-button.danger { color: var(--danger); }
.mobile-back { display: none; }

.reader-header h2 {
  margin: 20px 0 18px;
  overflow-wrap: anywhere;
  font-size: 26px;
  font-weight: 650;
  line-height: 1.2;
  letter-spacing: 0;
}

.message-meta { display: grid; grid-template-columns: 36px minmax(0, 1fr) auto; align-items: center; gap: 10px; }
.avatar { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; color: white; background: #4d6d83; font-weight: 700; }
.sender-block { min-width: 0; }
.sender-block strong { display: inline; overflow-wrap: anywhere; }
.recipient-toggle { margin-left: 5px; padding: 0; border: 0; color: var(--accent); background: transparent; font-size: 11px; cursor: pointer; }
.recipient-details { margin-top: 5px; color: var(--muted); font-size: 11px; line-height: 1.45; overflow-wrap: anywhere; }
.message-meta time { align-self: start; color: var(--muted); font-size: 11px; white-space: nowrap; }

.attachment-strip { display: flex; gap: 8px; overflow-x: auto; padding: 10px 28px; border-bottom: 1px solid var(--line); background: white; }
.attachment-link { display: inline-flex; align-items: center; gap: 6px; min-height: 32px; padding: 0 10px; border: 1px solid var(--line); border-radius: 5px; color: var(--ink); text-decoration: none; white-space: nowrap; }
.attachment-link:hover { border-color: var(--accent); }

.message-body { width: 100%; height: 100%; border: 0; background: white; }

.composer {
  width: min(680px, calc(100vw - 28px));
  height: min(720px, calc(100dvh - 40px));
  padding: 0;
  border: 0;
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.composer::backdrop { background: rgba(22, 32, 31, 0.42); }
.composer form { display: grid; grid-template-rows: 48px auto minmax(180px, 1fr) auto 54px; height: 100%; }
.composer header, .composer footer { display: flex; align-items: center; gap: 8px; padding: 0 14px; }
.composer header { border-bottom: 1px solid var(--line); background: #edf4f2; }
.composer header > div { min-width: 0; }
.composer header strong { display: block; }
.composer header span { display: block; color: var(--muted); font-size: 10px; }
.composer header .icon-button { margin-left: auto; }

.compose-fields { position: relative; border-bottom: 1px solid var(--line); }
.compose-fields label { display: grid; grid-template-columns: 54px minmax(0, 1fr); align-items: center; min-height: 39px; padding: 0 14px; border-bottom: 1px solid #eef1f0; }
.compose-fields label span { color: var(--muted); font-size: 12px; }
.compose-fields input { width: 100%; height: 38px; border: 0; outline: 0; min-width: 0; }
.compose-fields input[readonly] { color: var(--muted); }
.subject-field { border-bottom: 0 !important; }
.field-toggle { position: absolute; top: 42px; right: 10px; min-height: 26px; padding: 0 7px; border: 0; color: var(--accent); font-size: 11px; }
.optional-fields:not(.hidden) + .field-toggle { top: 120px; }

#compose-body { width: 100%; height: 100%; padding: 18px; resize: none; border: 0; outline: 0; line-height: 1.55; }
.selected-files { display: flex; gap: 6px; overflow-x: auto; padding: 0 14px; }
.file-chip { align-self: center; max-width: 180px; overflow: hidden; padding: 5px 8px; border-radius: 4px; background: var(--soft); color: var(--muted); font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.composer footer { border-top: 1px solid var(--line); }
.send-button { min-width: 76px; min-height: 36px; border: 0; border-radius: 5px; color: white; background: var(--accent); font-weight: 650; cursor: pointer; }
.send-button:hover { background: var(--accent-hover); }
.send-button:disabled { opacity: 0.55; cursor: wait; }
.attach-button { display: inline-flex; align-items: center; }
.attach-button input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.compose-spacer { flex: 1; }

.toast-region { position: fixed; right: 20px; bottom: 20px; display: grid; gap: 8px; z-index: 100; }
.toast { max-width: min(360px, calc(100vw - 40px)); padding: 11px 14px; border-radius: 6px; color: white; background: #263d39; box-shadow: var(--shadow); }
.toast.error { background: #8e3030; }

.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

@media (max-width: 980px) {
  .app-shell { grid-template-columns: 200px minmax(300px, 350px) minmax(360px, 1fr); }
  .reader-header { padding-inline: 20px; }
}

@media (max-width: 760px) {
  .app-shell { display: block; position: relative; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 250px; transform: translateX(-102%); box-shadow: var(--shadow); transition: transform 180ms ease; }
  body.sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-scrim { position: fixed; inset: 0; z-index: 15; background: rgba(22, 32, 31, 0.35); }
  body.sidebar-open .sidebar-scrim { display: block !important; }
  .mail-list-panel, .reader-panel { position: absolute; inset: 0; border: 0; }
  .reader-panel { transform: translateX(100%); transition: transform 180ms ease; z-index: 5; }
  body.reading .reader-panel { transform: translateX(0); }
  .list-header { height: 154px; padding: 12px 14px; }
  .message-list { height: calc(100dvh - 154px); }
  .mobile-menu { display: inline-grid; place-items: center; width: 34px; height: 34px; margin-right: 8px; border: 0; background: transparent; font-size: 18px; cursor: pointer; }
  .mobile-back { display: inline-grid; }
  .reader-header { padding: 10px 16px 14px; }
  .reader-header h2 { margin: 14px 0; font-size: 21px; }
  .reader-toolbar { overflow-x: auto; }
  .message-meta { grid-template-columns: 32px minmax(0, 1fr); }
  .avatar { width: 32px; height: 32px; }
  .message-meta time { grid-column: 2; }
  .attachment-strip { padding-inline: 16px; }
  .composer { width: 100vw; height: 100dvh; max-width: none; max-height: none; border-radius: 0; }
  .composer form { grid-template-rows: 48px auto minmax(120px, 1fr) auto 58px; }
  .composer footer { padding-bottom: env(safe-area-inset-bottom); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
}
