:root {
  color-scheme: dark;
  --bg: #12100e;
  --card: #1c1814;
  --text: #f4efe6;
  --muted: #a89f93;
  --accent: #d4a06a;
  --border: #3a342c;
  --surface: #26211c;
  --unread: #8b5a2b;
  --danger: #c45c4a;
  --ok: #6aa84f;
  --font: Georgia, 'Times New Roman', serif;
  --sans: 'Segoe UI', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }
body {
  font-family: var(--font);
  min-height: 100vh;
  min-height: 100dvh;
}

[hidden], .is-off { display: none !important; }

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background-image:
    linear-gradient(90deg, rgba(255,255,255,.04) 1px, transparent 1px),
    linear-gradient(rgba(255,255,255,.04) 1px, transparent 1px);
  background-size: 56px 56px;
}

.login-card, .compose {
  width: min(420px, 100%);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.4rem 1.25rem;
}
.login-logo {
  display: block;
  width: 88px;
  height: 88px;
  object-fit: cover;
  object-position: center 42%;
  margin: 0 0 .85rem;
  border-radius: 18px;
  background: #1a140e;
}

.brand, .domain, .hint, .field span, .status, .badge, .meta, .preview, .folder {
  font-family: var(--sans);
}

.brand { margin: 0; color: var(--accent); font-size: .78rem; letter-spacing: .08em; text-transform: uppercase; }
.login-card h1, .compose h2, .pane-head h2 { margin: .2rem 0 .15rem; font-weight: 400; }
.login-card h1 { margin-bottom: 1rem; }
.hint { margin: 0 0 1rem; color: var(--muted); font-size: .85rem; }

.field { display: flex; flex-direction: column; gap: .35rem; margin-bottom: .7rem; }
.field span { font-size: .78rem; color: var(--muted); font-weight: 600; }
.field input, .field select, .field textarea {
  font: inherit;
  font-family: var(--sans);
  font-size: .95rem;
  padding: .55rem .7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
}

.btn-primary, .btn-ghost, .icon-close {
  font-family: var(--sans);
  cursor: pointer;
  border-radius: 6px;
}
.btn-primary {
  border: 0;
  background: var(--accent);
  color: #1a140e;
  font-weight: 600;
  padding: .6rem .9rem;
}
.btn-ghost {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  padding: .45rem .7rem;
}
.form-msg { color: var(--danger); font-family: var(--sans); font-size: .82rem; }
.form-msg.ok { color: var(--ok); }

.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
  min-height: 100dvh;
}

.nav-scrim {
  display: none;
  border: 0;
  padding: 0;
  background: rgba(0, 0, 0, .45);
}

.sidebar {
  border-right: 1px solid var(--border);
  padding: 1rem .9rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;
  background: var(--card);
}
.brand-row {
  display: flex;
  align-items: center;
  gap: .65rem;
}
.brand-logo {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  object-fit: cover;
  object-position: center 42%;
  background: #1a140e;
  flex: 0 0 44px;
}
.brand-text { display: flex; flex-direction: column; gap: .1rem; min-width: 0; }
.brand-row strong { font-size: 1.15rem; }
.domain { color: var(--muted); font-size: .75rem; }
.compose-btn { width: 100%; }
.folders { display: flex; flex-direction: column; gap: .25rem; flex: 1; }
.folder {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: .55rem .65rem;
  border-radius: 999px;
  cursor: pointer;
}
.folder.is-active, .folder:hover { background: var(--surface); }
.badge {
  background: var(--unread);
  color: #fff;
  border-radius: 999px;
  min-width: 1.3rem;
  text-align: center;
  font-size: .72rem;
  padding: .1rem .35rem;
}
.status { margin-top: auto; font-size: .75rem; color: var(--muted); line-height: 1.35; }
.status.ok { color: var(--ok); }
.status.bad { color: var(--danger); }

.workspace { min-width: 0; display: flex; flex-direction: column; min-height: 100dvh; }
.topbar {
  display: flex;
  align-items: center;
  gap: .65rem;
  padding: .7rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--card);
}
.topbar h2 { margin: 0; font-size: 1.15rem; flex: 1; }
.hamburger {
  display: none;
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  padding: 11px 10px;
  cursor: pointer;
}
.hamburger span {
  display: block;
  height: 2px;
  background: var(--text);
  border-radius: 2px;
}
.hamburger span + span { margin-top: 6px; }
.hamburger:hover { background: var(--surface); }

.panes {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(280px, 360px) 1fr;
}

.list-pane, .reader-pane { min-width: 0; display: flex; flex-direction: column; }
.list-pane { border-right: 1px solid var(--border); }
.contact-add {
  display: grid;
  grid-template-columns: 1fr 1.3fr auto;
  gap: .4rem;
  padding: .75rem .85rem;
  border-bottom: 1px solid var(--border);
  background: var(--card);
}
.contact-add.is-off { display: none !important; }
.contact-add input {
  font: inherit;
  font-family: var(--sans);
  font-size: .9rem;
  padding: .5rem .65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  min-width: 0;
}
.contact-add-msg {
  margin: 0;
  padding: .35rem .85rem 0;
  font-family: var(--sans);
  font-size: .8rem;
}
.contact-add-msg.is-off { display: none !important; }
#compose-contact {
  margin-top: .4rem;
}
.pane-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .85rem 1rem;
  border-bottom: 1px solid var(--border);
}
.pane-head h2 { margin: 0; font-size: 1.05rem; }

.list { overflow: auto; flex: 1; }
.item {
  width: 100%;
  display: grid;
  grid-template-columns: 40px 1fr;
  column-gap: .7rem;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: inherit;
  padding: .85rem 1rem;
  cursor: pointer;
  font-family: var(--sans);
}
.item-body { min-width: 0; }
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: .95rem;
  color: #fff;
  flex: 0 0 40px;
  overflow: hidden;
  background: var(--unread);
}
.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 42%;
}
.avatar--lg { width: 48px; height: 48px; font-size: 1.1rem; }
.reader-head {
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: .75rem;
  align-items: start;
  margin-bottom: 1rem;
}
.reader-head .meta { margin-bottom: 0; }
.reader-head h1 { margin: 0 0 .35rem; }
.item.is-active { background: var(--surface); }
.item.unread .from { font-weight: 700; }
.item .from { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .subject {
  color: var(--muted);
  font-size: .86rem;
  margin-top: .15rem;
  display: flex;
  gap: .4rem;
  align-items: center;
  min-width: 0;
}
.item .subj {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.item .meta { color: var(--muted); font-size: .72rem; margin-top: .25rem; }
.item .clip {
  flex: 0 0 auto;
  font-size: .72rem;
  color: var(--accent);
}

.reader-pane { padding: 1.1rem 1.25rem 1.5rem; overflow: auto; }
.empty-reader {
  margin: auto;
  color: var(--muted);
  font-family: var(--sans);
}
.reader h1 { margin: 0 0 .35rem; font-size: 1.35rem; font-weight: 400; word-break: break-word; }
.reader .meta {
  color: var(--muted);
  margin-bottom: 1rem;
  line-height: 1.45;
  font-family: var(--sans);
}
.reader iframe {
  display: block;
  width: 100%;
  min-height: 50vh;
  height: 62vh;
  border: 0;
  background: transparent;
  color-scheme: dark;
  border-radius: 8px;
}
.reader .body-text {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--sans);
  font-size: 1.02rem;
  line-height: 1.55;
  color: var(--text);
}
.reader-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.btn-back { display: none; }

.mail-files, .attach-chips {
  list-style: none;
  margin: 0 0 1rem;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: .45rem;
  font-family: var(--sans);
}
.attach-chip {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  max-width: 100%;
  padding: .45rem .7rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font-size: .82rem;
}
.attach-chip strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}
.attach-chip span { color: var(--muted); white-space: nowrap; }
.chip-remove {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  padding: 0 .1rem;
  cursor: pointer;
}

.attach-zone {
  margin: .15rem 0 .7rem;
  padding: .75rem;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.attach-zone.is-over { border-color: var(--accent); }
.attach-actions { display: flex; flex-wrap: wrap; align-items: center; gap: .55rem; }
.attach-hint { color: var(--muted); font-family: var(--sans); font-size: .8rem; }
.attach-chips { margin: .65rem 0 0; }

.compose { width: min(640px, 96vw); padding: 0; border: 1px solid var(--border); background: var(--card); color: var(--text); }
.compose::backdrop { background: rgba(0,0,0,.55); }
.compose-form { padding: 1rem 1.1rem 1.15rem; display: flex; flex-direction: column; }
.compose header, .compose footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .75rem;
}
.compose header { margin-bottom: .6rem; }
.compose h2 { margin: 0; }
.icon-close {
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 1.4rem;
  line-height: 1;
}
.compose .grow textarea { min-height: 180px; resize: vertical; }
.compose footer { margin-top: .4rem; }

.toast {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: calc(1.4rem + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  width: min(420px, calc(100vw - 2rem));
  padding: .85rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-family: var(--sans);
  font-size: .9rem;
  box-shadow: 0 12px 32px rgba(0,0,0,.4);
  cursor: pointer;
}

.item .kind {
  display: inline-block;
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--accent);
  margin-right: .35rem;
}

.btn-primary, .btn-ghost, .hamburger, .folder, .item, .icon-close {
  touch-action: manipulation;
}

@media (max-width: 960px) {
  .app { grid-template-columns: 1fr; }
  .hamburger { display: flex; flex-direction: column; justify-content: center; }
  .nav-scrim {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 20;
  }
  .app.nav-open .nav-scrim { display: block; }
  .sidebar {
    position: fixed;
    z-index: 30;
    inset: 0 auto 0 0;
    width: min(320px, 86vw);
    padding-top: calc(1rem + env(safe-area-inset-top, 0px));
    padding-bottom: calc(.9rem + env(safe-area-inset-bottom, 0px));
    transform: translateX(-105%);
    transition: transform .22s ease;
    box-shadow: none;
  }
  .app.nav-open .sidebar {
    transform: none;
    box-shadow: 8px 0 28px rgba(0,0,0,.35);
  }
  .topbar {
    padding-top: calc(.7rem + env(safe-area-inset-top, 0px));
    position: sticky;
    top: 0;
    z-index: 10;
  }
  .panes { grid-template-columns: 1fr; }
  .list-pane { min-height: 0; }
  .reader-pane {
    display: none;
    border-top: 0;
    padding: 1rem 1rem calc(1.2rem + env(safe-area-inset-bottom, 0px));
  }
  .app.show-reader .list-pane,
  .app.show-reader .topbar { display: none; }
  .app.show-reader .reader-pane { display: flex; flex: 1; min-height: 100dvh; }
  .btn-back { display: inline-flex; }
  .item { padding: .95rem 1rem; min-height: 72px; }
  .folder, .btn-ghost, .btn-primary { min-height: 44px; }
  .reader iframe { height: 58vh; min-height: 240px; }
  .attach-hint { display: none; }
}

@media (max-width: 720px) {
  .login-wrap { padding: calc(1.2rem + env(safe-area-inset-top, 0px)) 1rem calc(1.2rem + env(safe-area-inset-bottom, 0px)); }
  .login-card { padding: 1.2rem 1rem; }
  .compose {
    width: 100vw;
    max-width: none;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }
  .compose::backdrop { background: var(--bg); }
  .compose-form {
    min-height: 100dvh;
    padding: calc(.85rem + env(safe-area-inset-top, 0px)) 1rem calc(.9rem + env(safe-area-inset-bottom, 0px));
  }
  .compose .grow textarea { min-height: 28vh; }
  .compose footer {
    position: sticky;
    bottom: 0;
    background: var(--card);
    padding-top: .7rem;
  }
  .field input, .field select, .field textarea { font-size: 16px; padding: .7rem .75rem; }
  .contact-add { grid-template-columns: 1fr; }
}
