:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f1f3f5;
  --border: #e3e6ea;
  --text: #1c2024;
  --muted: #6b7280;
  --accent: #059669;
  --accent-strong: #047857;
  --accent-soft: #e7f6ef;
  --selected: #e8f0fe;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 8px 24px rgb(0 0 0 / 0.08);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0d10;
    --panel: #111418;
    --panel-2: #181c21;
    --border: #252a31;
    --text: #e6e8eb;
    --muted: #8b939e;
    --accent: #34d399;
    --accent-strong: #6ee7b7;
    --accent-soft: #0f2a21;
    --selected: #172233;
    --danger: #f87171;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 8px 24px rgb(0 0 0 / 0.5);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font:
    14px/1.45 system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    sans-serif;
}

[hidden] {
  display: none !important;
}

code {
  font-family: "SF Mono", ui-monospace, "Fira Code", Menlo, monospace;
  font-size: 0.92em;
}

h1,
h2,
h3 {
  margin: 0;
}

.sprite {
  position: absolute;
  width: 0;
  height: 0;
}

.icon {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 12.5px;
}

.center {
  text-align: center;
}

.pad {
  padding: 8px 12px;
}

/* Buttons & inputs ------------------------------------------------------ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition:
    background 0.15s,
    border-color 0.15s;
}

.btn:hover {
  background: var(--panel-2);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

@media (prefers-color-scheme: dark) {
  .btn.primary {
    color: #04130d;
  }
}

.btn.primary:hover {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.btn.block {
  width: 100%;
}

.btn.small {
  padding: 4px 10px;
  font-size: 12.5px;
}

.btn:disabled {
  opacity: 0.6;
  cursor: progress;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.icon-btn:hover,
.icon-btn[aria-pressed="true"] {
  background: var(--panel-2);
  color: var(--text);
}

.icon-btn.danger:hover {
  color: var(--danger);
}

.link {
  border: 0;
  padding: 0;
  background: none;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

input[type="text"],
input[type="password"],
input[type="search"] {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
}

input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

label {
  display: block;
  margin: 14px 0 6px;
  font-weight: 500;
}

label.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 400;
  color: var(--muted);
}

.form-error {
  margin: 12px 0 0;
  padding: 8px 12px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
}

/* Brand ------------------------------------------------------------------ */

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-weight: 700;
  font-size: 16px;
  text-decoration: none;
  white-space: nowrap;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
}

/* Login ------------------------------------------------------------------ */

.login {
  display: grid;
  place-items: center;
  min-height: 100%;
  padding: 24px 16px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 32px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.login-card h1 {
  margin: 24px 0 6px;
  font-size: 22px;
}

.login-card p {
  margin: 0 0 8px;
}

.login-card .btn.primary {
  margin-top: 18px;
  padding: 10px;
}

.login-card .center {
  margin-top: 18px;
}

/* App shell -------------------------------------------------------------- */

.app {
  display: grid;
  grid-template-columns: 260px minmax(300px, 380px) minmax(0, 1fr);
  grid-template-rows: 56px 1fr;
  grid-template-areas:
    "top top top"
    "side list reader";
  height: 100%;
}

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.topbar .brand {
  width: 236px;
}

.nav-toggle {
  display: none;
}

.search {
  position: relative;
  flex: 1;
  min-width: 0;
  max-width: 640px;
  margin: 0;
}

.search .icon {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  width: 16px;
  height: 16px;
}

.search input {
  padding-left: 34px;
  background: var(--panel-2);
  border-color: transparent;
}

.topbar #refresh {
  margin-left: auto;
}

.spinning .icon {
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Sidebar ---------------------------------------------------------------- */

.sidebar {
  grid-area: side;
  overflow-y: auto;
  padding: 14px 10px;
  border-right: 1px solid var(--border);
  background: var(--bg);
}

.sidebar > .btn {
  margin-bottom: 14px;
}

.sidebar-heading {
  margin: 18px 12px 6px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.folders {
  list-style: none;
  margin: 0;
  padding: 0;
}

.folders li {
  position: relative;
}

.folder {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 7px 12px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.folder:hover {
  background: var(--panel-2);
}

.folder.active {
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 600;
}

.folder .icon {
  color: var(--muted);
}

.folder.active .icon {
  color: inherit;
}

.folder-name {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.folder-title,
.folder-sub {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.folder-sub {
  font-size: 12px;
  font-weight: 400;
  color: var(--muted);
}

.badge {
  min-width: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 11.5px;
  font-weight: 600;
  line-height: 20px;
  text-align: center;
}

.folders .copy {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  opacity: 0;
  background: var(--panel-2);
}

.folders li:hover .copy,
.folders .copy:focus-visible {
  opacity: 1;
}

.folders li:hover .badge {
  visibility: hidden;
}

/* Message list ----------------------------------------------------------- */

.list {
  grid-area: list;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
  background: var(--panel);
}

.list-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.list-header h2 {
  overflow: hidden;
  font-size: 16px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.messages {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 0;
}

.message {
  display: flex;
  gap: 12px;
  width: 100%;
  padding: 12px 16px;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-left: 3px solid transparent;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.message:hover {
  background: var(--panel-2);
}

.message.selected {
  background: var(--selected);
  border-left-color: var(--accent);
}

.message-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.message-top {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.message-from {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-date {
  flex-shrink: 0;
  font-size: 12px;
  color: var(--muted);
}

.message-subject,
.message-snippet,
.message-to {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-snippet,
.message-to {
  font-size: 13px;
  color: var(--muted);
}

.message-to {
  font-size: 12px;
}

.message.unread .message-from,
.message.unread .message-subject {
  font-weight: 700;
}

.message.unread .message-date {
  color: var(--accent);
  font-weight: 600;
}

.avatar {
  display: grid;
  place-items: center;
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: hsl(var(--hue, 160) 55% 88%);
  color: hsl(var(--hue, 160) 50% 28%);
  font-weight: 600;
}

@media (prefers-color-scheme: dark) {
  .avatar {
    background: hsl(var(--hue, 160) 35% 22%);
    color: hsl(var(--hue, 160) 60% 78%);
  }
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 48px 24px;
  text-align: center;
}

.empty .icon {
  width: 36px;
  height: 36px;
  margin-bottom: 8px;
  color: var(--muted);
  stroke-width: 1.5;
}

.empty p {
  margin: 0;
}

.empty .btn {
  margin-top: 12px;
}

/* Reader ----------------------------------------------------------------- */

.reader {
  grid-area: reader;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  background: var(--panel);
}

.reader-empty {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: var(--muted);
}

.reader-empty .icon {
  width: 48px;
  height: 48px;
  margin-bottom: 8px;
  stroke-width: 1.25;
}

.reader-empty p {
  margin: 0;
}

.reader-toolbar {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.reader-toolbar .back {
  display: none;
}

.spacer {
  flex: 1;
}

.segmented {
  display: inline-flex;
  margin-right: 6px;
  padding: 2px;
  border-radius: 8px;
  background: var(--panel-2);
}

.segmented button {
  padding: 4px 10px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
}

.segmented button[aria-pressed="true"] {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.1);
}

.reader-header {
  padding: 20px 24px 16px;
}

.reader-subject {
  margin-bottom: 16px;
  font-size: 20px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.reader-meta {
  display: flex;
  align-items: center;
  gap: 12px;
}

.reader-people {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.reader-from {
  font-weight: 600;
}

.reader-date {
  flex-shrink: 0;
  font-size: 13px;
  text-align: right;
}

.reader-loading {
  padding: 24px;
}

.notice {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 24px 12px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
  font-size: 13px;
}

.notice span {
  flex: 1;
}

.headers {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 16px;
  margin: 0 24px 16px;
  padding: 12px 14px;
  max-height: 280px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
  font-family: "SF Mono", ui-monospace, Menlo, monospace;
  font-size: 12px;
}

.headers dt {
  color: var(--muted);
}

.headers dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.email-frame-wrap {
  flex: 1;
  display: flex;
  min-height: 420px;
  margin: 0 24px 24px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: #fff;
}

.email-frame {
  flex: 1;
  width: 100%;
  border: 0;
  background: #fff;
}

.text-body {
  margin: 0 24px 24px;
  font-family: "SF Mono", ui-monospace, Menlo, monospace;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.text-body a {
  color: var(--accent);
}

/* Dialogs & toast -------------------------------------------------------- */

.dialog {
  width: min(440px, calc(100% - 32px));
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
}

.dialog::backdrop {
  background: rgb(0 0 0 / 0.4);
}

.dialog h2 {
  margin-bottom: 6px;
  font-size: 18px;
}

.dialog p {
  margin: 0;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.token-box {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
  padding: 8px 8px 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
}

.token-box code {
  flex: 1;
  overflow-wrap: anywhere;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 10;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 8px;
  background: var(--text);
  color: var(--panel);
  box-shadow: var(--shadow);
  transform: translateX(-50%);
  overflow-wrap: anywhere;
}

.toast.error {
  background: var(--danger);
  color: #fff;
}

.scrim {
  display: none;
}

/* Responsive ------------------------------------------------------------- */

@media (max-width: 1100px) {
  .app {
    grid-template-columns: 220px minmax(260px, 320px) minmax(0, 1fr);
  }

  .topbar .brand {
    width: auto;
    margin-right: 12px;
  }
}

@media (max-width: 760px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "top"
      "list";
  }

  .topbar {
    padding: 0 8px;
  }

  .topbar .brand {
    display: none;
  }

  .nav-toggle {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 5;
    width: min(300px, 85%);
    transform: translateX(-100%);
    transition: transform 0.2s;
  }

  .nav-open .sidebar {
    transform: none;
  }

  .nav-open .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 4;
    background: rgb(0 0 0 / 0.4);
  }

  .reader {
    display: none;
  }

  .reading .list {
    display: none;
  }

  .reading .reader {
    display: flex;
    grid-area: list;
  }

  .reader-toolbar .back {
    display: inline-flex;
  }

  .reader-header,
  .headers,
  .notice,
  .text-body {
    margin-left: 16px;
    margin-right: 16px;
  }

  .reader-header {
    padding: 16px 0 12px;
  }

  .email-frame-wrap {
    margin: 0 8px 16px;
  }

  .folders .copy {
    opacity: 1;
  }

  .folders li .badge {
    margin-right: 30px;
  }
}
