/*
 * maildeck UI.
 *
 * No build step on purpose: this is a self-hosted app that should deploy with
 * `git pull && systemctl restart`, not with a toolchain on the server.
 *
 * Density is the design goal. A mail client is a tool you look at for hours,
 * so the job is to fit a lot of information on screen without it feeling loud
 * — which mostly means restraint with colour and one clear accent.
 */

:root {
  color-scheme: light;

  --paper:        #fbfbfa;
  --surface:      #ffffff;
  --surface-2:    #f4f4f1;
  --surface-3:    #eaeae5;
  --ink:          #16181b;
  --ink-2:        #4a5159;
  --ink-3:        #838b94;
  --rule:         #e2e2dc;
  --rule-soft:    #eeeee9;

  --accent:       #1a5c8a;
  --accent-ink:   #ffffff;
  --accent-soft:  #e6eff6;
  --accent-line:  #b9d4e6;

  --unread:       #0f4f78;
  --star:         #c98a12;
  --danger:       #a52d1e;
  --danger-soft:  #fae9e6;
  --ok:           #1c6b45;
  --ok-soft:      #e3f1ea;

  --shadow-1: 0 1px 2px rgb(20 24 26 / 7%);
  --shadow-2: 0 4px 16px rgb(20 24 26 / 10%), 0 1px 3px rgb(20 24 26 / 8%);

  --w-sidebar: 15rem;
  --w-list: 23rem;
  --radius: 6px;

  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;
    --paper:       #0e1013;
    --surface:     #15181c;
    --surface-2:   #1c2025;
    --surface-3:   #262b31;
    --ink:         #e8eaed;
    --ink-2:       #a7aeb6;
    --ink-3:       #757d86;
    --rule:        #282d33;
    --rule-soft:   #1f2429;

    --accent:      #6bb0dd;
    --accent-ink:  #0e1013;
    --accent-soft: #14293a;
    --accent-line: #21455f;

    --unread:      #8ec5ea;
    --star:        #d9a83f;
    --danger:      #e8836f;
    --danger-soft: #2f1a16;
    --ok:          #5fc191;
    --ok-soft:     #12291f;

    --shadow-1: 0 1px 2px rgb(0 0 0 / 40%);
    --shadow-2: 0 4px 16px rgb(0 0 0 / 50%), 0 1px 3px rgb(0 0 0 / 40%);
  }
}

:root[data-theme='dark'] {
  color-scheme: dark;
  --paper: #0e1013; --surface: #15181c; --surface-2: #1c2025; --surface-3: #262b31;
  --ink: #e8eaed; --ink-2: #a7aeb6; --ink-3: #757d86;
  --rule: #282d33; --rule-soft: #1f2429;
  --accent: #6bb0dd; --accent-ink: #0e1013; --accent-soft: #14293a; --accent-line: #21455f;
  --unread: #8ec5ea; --star: #d9a83f;
  --danger: #e8836f; --danger-soft: #2f1a16; --ok: #5fc191; --ok-soft: #12291f;
  --shadow-1: 0 1px 2px rgb(0 0 0 / 40%);
  --shadow-2: 0 4px 16px rgb(0 0 0 / 50%), 0 1px 3px rgb(0 0 0 / 40%);
}

* { box-sizing: border-box; }

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

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

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

/* ------------------------------- login ---------------------------------- */

.login {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 1.5rem;
}

.login .card {
  width: min(24rem, 100%);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 10px;
  box-shadow: var(--shadow-2);
  padding: 1.75rem;
}

.login h1 {
  margin: 0 0 .2rem;
  font-size: 1.35rem;
  letter-spacing: -.02em;
}

.login label {
  display: block;
  margin-top: .9rem;
  font-size: .78rem;
  font-weight: 600;
  color: var(--ink-2);
  letter-spacing: .01em;
}

.login input {
  display: block;
  width: 100%;
  margin-top: .3rem;
  padding: .5rem .6rem;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  font-weight: 400;
  color: var(--ink);
}

.login .row { display: flex; gap: .75rem; }
.login .row label { flex: 1; }

.login button[type='submit'] {
  width: 100%;
  margin-top: 1.4rem;
  padding: .6rem;
  background: var(--accent);
  color: var(--accent-ink);
  border: 0;
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
}

.login button[type='submit']:hover { filter: brightness(1.08); }
.login button[type='submit']:disabled { opacity: .6; cursor: progress; }

.muted { color: var(--ink-3); font-size: .85rem; margin: 0 0 .4rem; }
.error { color: var(--danger); font-size: .85rem; margin: .8rem 0 0; }

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

.app {
  display: grid;
  grid-template-columns: var(--w-sidebar) var(--w-list) 1fr;
  height: 100vh;
  height: 100dvh;
}

/* ------------------------------- sidebar -------------------------------- */

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface-2);
  border-right: 1px solid var(--rule);
}

.brand {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .7rem .85rem;
  border-bottom: 1px solid var(--rule-soft);
}

.brand-mark {
  width: 1.35rem; height: 1.35rem;
  display: grid; place-items: center;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 4px;
  font-size: .7rem; font-weight: 800;
}

.brand-name { font-weight: 700; letter-spacing: -.01em; }

.compose-btn {
  margin: .7rem .85rem .3rem;
  padding: .5rem .75rem;
  display: flex; align-items: center; gap: .5rem;
  background: var(--accent);
  color: var(--accent-ink);
  border: 0; border-radius: var(--radius);
  font-weight: 600; cursor: pointer;
  box-shadow: var(--shadow-1);
}
.compose-btn:hover { filter: brightness(1.08); }

.folders {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: .5rem .5rem 1rem;
}

.folder {
  display: flex;
  align-items: center;
  gap: .5rem;
  width: 100%;
  padding: .32rem .55rem;
  border: 0;
  border-radius: var(--radius);
  background: none;
  color: var(--ink-2);
  text-align: left;
  cursor: pointer;
  font-size: .875rem;
}

.folder:hover { background: var(--surface-3); }

.folder[aria-current='true'] {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.folder-icon { flex: none; width: 1rem; opacity: .8; }
.folder-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.folder-count {
  flex: none;
  font-size: .72rem;
  font-variant-numeric: tabular-nums;
  color: var(--ink-3);
}
.folder[aria-current='true'] .folder-count { color: var(--accent); }
.folder-count.unread { color: var(--unread); font-weight: 700; }

.account-bar {
  border-top: 1px solid var(--rule-soft);
  padding: .5rem .85rem;
  display: flex;
  align-items: center;
  gap: .5rem;
  font-size: .78rem;
  color: var(--ink-3);
}
.account-bar span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

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

/* index progress */
.sync-bar { padding: .4rem .85rem .55rem; font-size: .72rem; color: var(--ink-3); }
.sync-track { height: 3px; background: var(--surface-3); border-radius: 2px; overflow: hidden; margin-top: .3rem; }
.sync-fill { height: 100%; background: var(--accent); transition: width .4s ease; }

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

/* min-width: 0 on every grid and flex child that holds ellipsised text.
   Without it their automatic minimum size is min-content, so a long subject
   or a wide toolbar pushes the whole column past the viewport instead of
   truncating — which is how a 390px screen ends up 428px wide. */
.list {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--surface);
  border-right: 1px solid var(--rule);
}

.list-header {
  display: flex;
  align-items: center;
  gap: .4rem;
  padding: .55rem .6rem;
  border-bottom: 1px solid var(--rule-soft);
}

.search-wrap { position: relative; flex: 1; min-width: 0; }

.search-wrap input {
  width: 100%;
  padding: .38rem .6rem .38rem 1.85rem;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: .85rem;
}
.search-wrap input:focus { background: var(--surface); border-color: var(--accent-line); outline: none; }

.search-icon {
  position: absolute; left: .5rem; top: 50%;
  transform: translateY(-50%);
  color: var(--ink-3);
  pointer-events: none;
}

.icon-btn {
  flex: none;
  display: grid; place-items: center;
  width: 1.9rem; height: 1.9rem;
  background: none; border: 0; border-radius: var(--radius);
  color: var(--ink-2); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn[aria-pressed='true'] { background: var(--accent-soft); color: var(--accent); }

.list-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }

.row-msg {
  display: grid;
  grid-template-columns: 1.1rem minmax(0, 1fr);
  gap: .1rem .5rem;
  width: 100%;
  padding: .5rem .7rem .55rem .5rem;
  border: 0;
  border-bottom: 1px solid var(--rule-soft);
  border-left: 2px solid transparent;
  background: none;
  text-align: left;
  cursor: pointer;
  color: inherit;
}

.row-msg:hover { background: var(--surface-2); }

.row-msg[aria-selected='true'] {
  background: var(--accent-soft);
  border-left-color: var(--accent);
}

/* The cursor is keyboard position; selection is what is open in the reader.
   They are usually the same row and sometimes not, so they need to look
   different or j/k navigation becomes disorienting. */
.row-msg.cursor { box-shadow: inset 0 0 0 1px var(--accent-line); }

.row-star {
  grid-row: 1 / span 2;
  align-self: start;
  padding: .1rem;
  background: none; border: 0;
  color: var(--ink-3);
  cursor: pointer;
  line-height: 0;
}
.row-star:hover { color: var(--star); }
.row-star.on { color: var(--star); }

.row-top { display: flex; align-items: baseline; gap: .5rem; min-width: 0; }

.row-from {
  flex: 1;
  min-width: 0;
  font-size: .875rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--ink-2);
}

.row-msg.unread .row-from { font-weight: 700; color: var(--ink); }
.row-msg.unread .row-subject { font-weight: 600; color: var(--ink); }

.row-date {
  flex: none;
  font-size: .72rem;
  color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}

.row-subject {
  font-size: .85rem;
  color: var(--ink-2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.row-preview {
  font-size: .78rem;
  color: var(--ink-3);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.row-badges { display: flex; gap: .3rem; align-items: center; }
.badge-attach { color: var(--ink-3); }
.badge-folder {
  font-size: .65rem; text-transform: uppercase; letter-spacing: .04em;
  color: var(--ink-3); background: var(--surface-3);
  padding: .05rem .3rem; border-radius: 3px;
}

.list-foot { padding: .8rem; text-align: center; color: var(--ink-3); font-size: .8rem; }

.empty-state {
  display: grid; place-items: center;
  height: 100%; padding: 2rem;
  text-align: center; color: var(--ink-3);
}
.empty-state h3 { margin: .5rem 0 .2rem; color: var(--ink-2); font-size: .95rem; }
.empty-state p { margin: 0; font-size: .82rem; max-width: 22rem; }

/* ------------------------------- reader --------------------------------- */

.reader {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--paper);
}

.reader-toolbar {
  display: flex;
  align-items: center;
  gap: .15rem;
  padding: .45rem .6rem;
  border-bottom: 1px solid var(--rule);
  background: var(--surface);
}
.reader-toolbar .spacer { flex: 1; }
.toolbar-sep { width: 1px; height: 1.1rem; background: var(--rule); margin: 0 .3rem; }

/* The reader does not scroll as a whole: the head stays put and the body
   iframe scrolls inside itself. That is forced by the sandbox — an opaque
   origin cannot be measured from the parent, so the frame cannot be sized to
   its content. It also means a hostile message cannot make the page grow
   without limit. */
.reader-scroll { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* The article is its own flex column rather than `display: contents`.
   Same layout, but contents-boxes have a history of dropping the element from
   the accessibility tree, and <article> is a landmark worth keeping. */
#reader-article { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#reader-article > .reader-head,
#reader-article > .banner,
#reader-article > .attachments { flex: none; }

.reader-head {
  padding: 1.1rem 1.4rem .8rem;
  border-bottom: 1px solid var(--rule-soft);
}

.reader-subject {
  margin: 0 0 .6rem;
  font-size: 1.15rem;
  line-height: 1.3;
  letter-spacing: -.015em;
  overflow-wrap: anywhere;
}

.reader-meta { display: flex; gap: .6rem; align-items: baseline; font-size: .82rem; flex-wrap: wrap; min-width: 0; }
.reader-from { font-weight: 600; }
.reader-addr { color: var(--ink-3); overflow-wrap: anywhere; }
.reader-when { margin-left: auto; color: var(--ink-3); flex: none; }
.reader-to { color: var(--ink-3); font-size: .8rem; margin-top: .15rem; overflow-wrap: anywhere; }

.banner {
  display: flex; align-items: center; gap: .6rem; flex-wrap: wrap;
  margin: .8rem 1.4rem 0;
  padding: .5rem .7rem;
  background: var(--surface-2);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  font-size: .8rem;
  color: var(--ink-2);
}

.attachments { display: flex; flex-wrap: wrap; gap: .4rem; padding: .7rem 1.4rem 0; }

.attachment {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .3rem .55rem;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  font-size: .78rem;
  color: var(--ink-2);
  text-decoration: none;
}
.attachment:hover { border-color: var(--accent-line); color: var(--accent); }
.attachment .size { color: var(--ink-3); font-variant-numeric: tabular-nums; }

.body-frame {
  flex: 1;
  width: 100%;
  min-height: 12rem;
  border: 0;
  background: var(--surface);
  display: block;
}

/* ------------------------------- compose -------------------------------- */

.overlay {
  position: fixed; inset: 0;
  background: rgb(10 12 14 / 45%);
  display: grid; place-items: end center;
  padding: 0;
  z-index: 40;
}

.compose {
  width: min(46rem, 100%);
  max-height: min(46rem, 92dvh);
  display: flex; flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-bottom: 0;
  border-radius: 10px 10px 0 0;
  box-shadow: var(--shadow-2);
}

.compose-head {
  display: flex; align-items: center; gap: .5rem;
  padding: .6rem .85rem;
  border-bottom: 1px solid var(--rule-soft);
  font-weight: 600;
}
.compose-head .spacer { flex: 1; }

.compose-fields { padding: .2rem .85rem; }

.field {
  display: flex; align-items: baseline; gap: .6rem;
  border-bottom: 1px solid var(--rule-soft);
}
.field label { flex: none; width: 3.2rem; font-size: .78rem; color: var(--ink-3); }
.field input {
  flex: 1; min-width: 0;
  padding: .5rem 0;
  background: none; border: 0;
  font-size: .875rem;
}
.field input:focus { outline: none; }
.field .link { flex: none; font-size: .75rem; }

.compose-body {
  flex: 1; min-height: 8rem;
  padding: .8rem .85rem;
  background: none;
  border: 0;
  resize: none;
  font-family: var(--font);
  font-size: .9rem;
  line-height: 1.6;
}
.compose-body:focus { outline: none; }

.compose-foot {
  display: flex; align-items: center; gap: .6rem;
  padding: .6rem .85rem;
  border-top: 1px solid var(--rule-soft);
}
.compose-foot .spacer { flex: 1; }

.btn {
  padding: .42rem .9rem;
  background: var(--surface-2);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: .85rem;
}
.btn:hover { background: var(--surface-3); }
.btn-primary {
  background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600;
}
.btn-primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ------------------------------- toasts --------------------------------- */

.toasts {
  position: fixed; bottom: 1rem; left: 50%;
  transform: translateX(-50%);
  display: flex; flex-direction: column; gap: .4rem;
  z-index: 60;
  pointer-events: none;
}

.toast {
  padding: .5rem .85rem;
  background: var(--ink);
  color: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
  font-size: .82rem;
  animation: toast-in .16s ease-out;
}
.toast.bad { background: var(--danger); color: #fff; }

@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

/* ------------------------------ shortcuts ------------------------------- */

.sheet {
  width: min(34rem, 100%);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 10px;
  box-shadow: var(--shadow-2);
  padding: 1.2rem 1.4rem 1.4rem;
  max-height: 85dvh; overflow-y: auto;
}
.overlay.center { place-items: center; padding: 1rem; }
.sheet h2 { margin: 0 0 .8rem; font-size: 1rem; }
.keys-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .1rem 1.5rem; }
.keys-grid h3 {
  grid-column: 1 / -1; margin: .8rem 0 .25rem;
  font-size: .7rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3);
}
.keys-row { display: flex; align-items: baseline; gap: .5rem; padding: .15rem 0; font-size: .82rem; }
.keys-row span { flex: 1; color: var(--ink-2); }
kbd {
  font-family: var(--mono); font-size: .72rem;
  padding: .1rem .35rem;
  background: var(--surface-2);
  border: 1px solid var(--rule);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--ink-2);
}

/* ------------------------------ responsive ------------------------------ */

.back-btn { display: none; }

@media (max-width: 1100px) {
  .app { grid-template-columns: var(--w-list) 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0;
    width: var(--w-sidebar);
    z-index: 30;
    transform: translateX(-100%);
    transition: transform .18s ease;
    box-shadow: var(--shadow-2);
  }
  .app.drawer-open .sidebar { transform: none; }
  .app.drawer-open::after {
    content: ''; position: fixed; inset: 0;
    background: rgb(10 12 14 / 35%); z-index: 20;
  }
  .menu-btn { display: grid !important; }
}

@media (min-width: 1101px) { .menu-btn { display: none !important; } }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  /* One pane at a time: the list, or the message. Anything else at this width
     is two half-useful columns instead of one usable one. */
  .app[data-pane='reader'] .list { display: none; }
  .app[data-pane='list'] .reader { display: none; }
  .back-btn { display: grid; }
  .reader-head { padding: .9rem 1rem .7rem; }
  .banner, .attachments { margin-inline: 1rem; padding-inline: 1rem; }
  .compose { max-height: 100dvh; height: 100dvh; border-radius: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
