:root {
  --paper: #fdf1f0;
  --card: #fffbfa;
  --ink: #2b2023;
  --muted: #7a5c62;
  --line: #f3dde0;
  --field-border: #9c7178;

  --pine: #a8395a;
  --pine-700: #8a2e4a;
  --pine-900: #3a1420;
  --river: #8a4a68;
  --river-700: #6e3a52;
  --clay: #9c5245;
  --gold: #b8823f;
  --gold-light: #f2b8c6;

  --radius: 12px;
  --sans: "Public Sans", system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  --display: "Zilla Slab", Georgia, "Times New Roman", serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; }
a { color: var(--river-700); }
:focus-visible { outline: 3px solid var(--pine-700); outline-offset: 2px; }

#app {
  display: grid;
  grid-template-columns: 220px 340px 1fr;
  height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--card);
  border-right: 1px solid var(--line);
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  overflow-y: auto;
}
.brand {
  font-family: var(--display);
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--pine-700);
  padding: 0 6px;
}
.brand small { display: block; font-family: var(--sans); font-weight: 600; font-size: .68rem; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); margin-top: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .5em;
  font-weight: 700; font-size: .94rem; padding: 10px 16px; border-radius: 10px;
  border: 2px solid transparent; cursor: pointer; text-decoration: none;
}
.btn-primary { background: var(--pine); color: #fff; }
.btn-primary:hover { background: var(--pine-700); }
.btn-ghost { background: var(--card); color: var(--pine-900); border-color: var(--field-border); }
.btn-ghost:hover { border-color: var(--pine-900); }
.btn-sm { padding: 6px 12px; font-size: .82rem; }
.btn-icon { padding: 6px 8px; }

.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-group h3 { font-size: .7rem; letter-spacing: .1em; text-transform: uppercase; color: var(--muted); margin: 0 0 6px 6px; }
.nav-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 9px 10px; border-radius: 8px; cursor: pointer; color: var(--ink);
  font-size: .93rem; font-weight: 600; background: transparent; border: none; text-align: left; width: 100%;
}
.nav-item:hover { background: #f7e1e6; }
.nav-item.active { background: var(--pine); color: #fff; }
.nav-item .count { font-size: .78rem; font-weight: 700; opacity: .85; }
.nav-item .dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 6px; }

.identity { margin-top: auto; font-size: .78rem; color: var(--muted); padding: 0 6px; }

/* ---------- Message list ---------- */
.list-pane { border-right: 1px solid var(--line); display: flex; flex-direction: column; background: var(--paper); }
.list-toolbar { display: flex; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--line); background: var(--card); }
.search-input {
  flex: 1; padding: 9px 12px; border-radius: 999px; border: 1px solid var(--field-border);
  font: inherit; font-size: .9rem; background: #fffdf7; color: var(--ink);
}
.message-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.message-row {
  display: grid; grid-template-columns: auto 1fr auto; align-items: start; gap: 4px 10px;
  padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; background: var(--paper);
}
.message-row:hover { background: #fbe7ec; }
.message-row.active { background: #f7dbe2; }
.message-row.unread .from, .message-row.unread .subject { font-weight: 800; }
.message-row .star { grid-row: 1 / 3; background: none; border: none; cursor: pointer; color: var(--field-border); font-size: 1.1rem; padding: 2px; }
.message-row .star.on { color: var(--gold); }
.message-row .from { font-size: .88rem; color: var(--ink); }
.message-row .time { font-size: .76rem; color: var(--muted); white-space: nowrap; }
.message-row .subject { grid-column: 2 / 3; font-size: .88rem; color: var(--ink); }
.message-row .snippet { grid-column: 2 / 3; font-size: .82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-badge { font-size: .72rem; font-weight: 700; color: var(--pine-700); background: #f7e3e8; border-radius: 999px; padding: 1px 7px; margin-left: 6px; }
.empty-state { padding: 40px 20px; text-align: center; color: var(--muted); font-size: .92rem; }

/* ---------- Detail pane ---------- */
.detail-pane { overflow-y: auto; background: var(--paper); }
.detail-header { padding: 20px 28px; border-bottom: 1px solid var(--line); background: var(--card); position: sticky; top: 0; }
.detail-header h1 { font-family: var(--display); font-size: 1.3rem; margin: 0 0 8px; }
.detail-actions { display: flex; gap: 8px; }
.thread-messages { padding: 20px 28px; display: flex; flex-direction: column; gap: 16px; }
.msg-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.msg-card.out { border-left: 4px solid var(--pine); }
.msg-card.in { border-left: 4px solid var(--river); }
.msg-card .meta { display: flex; justify-content: space-between; gap: 10px; font-size: .84rem; color: var(--muted); margin-bottom: 10px; }
.msg-card .meta b { color: var(--ink); }
.msg-card .body { font-size: .94rem; line-height: 1.6; white-space: pre-wrap; }
.msg-card .body.html { white-space: normal; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.attachment-chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: .82rem; padding: 6px 10px;
  border-radius: 999px; background: var(--paper); border: 1px solid var(--line); color: var(--ink); text-decoration: none;
}
.attachment-chip:hover { border-color: var(--field-border); }

.reply-box { padding: 0 28px 28px; }
.reply-box textarea {
  width: 100%; min-height: 100px; font: inherit; font-size: .94rem; padding: 12px 14px;
  border: 1px solid var(--field-border); border-radius: 10px; background: #fffdf7; color: var(--ink); resize: vertical;
}
.reply-box .row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }

/* ---------- Compose modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(58, 20, 32, .35); display: flex;
  align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal { background: var(--card); border-radius: 16px; width: min(640px, 100%); max-height: 90vh; overflow-y: auto; padding: 24px; box-shadow: 0 20px 60px -20px rgba(58,20,32,.5); }
.modal h2 { font-family: var(--display); margin: 0 0 16px; font-size: 1.25rem; }
.field { margin-bottom: 14px; }
.field label { display: block; font-weight: 600; font-size: .84rem; margin-bottom: 5px; color: var(--muted); }
.field input[type=text], .field input[type=email], .field textarea, .field input[type=file] {
  width: 100%; font: inherit; padding: 10px 12px; border: 1px solid var(--field-border); border-radius: 8px;
  background: #fffdf7; color: var(--ink);
}
.field textarea { min-height: 160px; resize: vertical; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.hint { font-size: .78rem; color: var(--muted); margin-top: 6px; }

.hidden { display: none !important; }

@media (max-width: 980px) {
  #app { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr; height: auto; min-height: 100vh; }
  .sidebar { flex-direction: row; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--line); }
  .sidebar .nav-group { flex-direction: row; }
  .identity { display: none; }
  .list-pane { border-right: none; border-bottom: 1px solid var(--line); max-height: 40vh; }
}
