:root {
  color-scheme: dark;
  --bg: #292d4e;
  --bg-deep: #1f2240;
  --panel: #30355c;
  --panel-2: #383e69;
  --hover: #3b4170;
  --selected: #464d85;
  --line: #424879;
  --text: #e9eaf5;
  --muted: #a4a8cb;
  --faint: #7c81a8;
  --accent: #8e97ff;
  --accent-strong: #6f7af7;
  --star: #ffcf5c;
  --danger: #ff7b88;
  --ok: #6fdba5;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", "Segoe UI", Roboto, "Noto Sans KR", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ───── 버튼 ───── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 16px; border-radius: 8px; border: 1px solid transparent;
  background: var(--panel-2); cursor: pointer; white-space: nowrap; user-select: none;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent-strong); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--accent); }
.btn-ghost { background: transparent; border-color: var(--line); }
.btn-ghost:hover { background: var(--hover); }
.btn-sm { height: 30px; padding: 0 12px; font-size: 13px; }
.btn-block { width: 100%; }
.btn-danger { background: #a83a4a; color: #fff; }
.btn-danger:hover { background: #c04456; }
.btn-danger-text { color: var(--danger); }
.icon-btn {
  width: 34px; height: 34px; border-radius: 8px; border: 0; background: transparent;
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; font-size: 16px; color: var(--muted);
  flex: 0 0 auto;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.link-btn { background: none; border: 0; color: var(--accent); cursor: pointer; font-size: 12px; padding: 4px 6px; white-space: nowrap; }

/* ───── 로그인 ───── */
.login { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px; background: radial-gradient(circle at 30% 20%, #343a6a, var(--bg-deep) 70%); }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.login-card .brand { margin-bottom: 22px; font-size: 18px; }
.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.field input { width: 100%; height: 42px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg-deep); outline: none; }
.field input:focus { border-color: var(--accent); }
.form-error { color: var(--danger); font-size: 13px; margin: 0 0 12px; }

.brand { font-weight: 700; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; }
.brand-mark { display: inline-flex; width: 28px; height: 28px; border-radius: 8px; background: var(--accent-strong); color: #fff; align-items: center; justify-content: center; font-size: 15px; }

/* ───── 레이아웃 ───── */
.app { display: grid; grid-template-columns: 236px minmax(320px, 400px) 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--bg-deep); display: flex; flex-direction: column; padding: 14px 10px; overflow-y: auto; min-height: 0; }
.sidebar-head { padding: 4px 8px 14px; }
.compose-btn { margin: 0 4px 14px; height: 42px; border-radius: 12px; }
.folders a, .mailboxes a {
  display: flex; align-items: center; gap: 10px; padding: 7px 12px; border-radius: 8px;
  color: var(--text); text-decoration: none; margin-bottom: 1px;
}
.folders a:hover, .mailboxes a:hover { background: var(--panel); }
.folders a.active, .mailboxes a.active { background: var(--selected); font-weight: 600; }
.folders .ico { width: 20px; text-align: center; opacity: .85; }
.folders .label, .mailboxes .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { font-size: 12px; color: var(--muted); font-weight: 600; }
.count.unread { color: var(--accent); }
.section-title { font-size: 11px; color: var(--faint); text-transform: uppercase; letter-spacing: .06em; padding: 18px 12px 6px; }
.mailboxes a { font-size: 13px; }
.sidebar-foot { margin-top: auto; padding: 14px 8px 4px; }
.scrim { display: none; }

.list-pane { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--bg); }
.list-head { display: flex; align-items: center; gap: 6px; padding: 10px 10px 6px; }
.search { flex: 1; }
.search input { width: 100%; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg-deep); padding: 0 12px; outline: none; }
.search input:focus { border-color: var(--accent); }
.list-toolbar { display: flex; align-items: center; gap: 6px; padding: 4px 10px 8px 14px; min-height: 42px; border-bottom: 1px solid var(--line); }
.list-title { font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-tools { display: flex; gap: 4px; }
.bulk-actions { display: flex; gap: 2px; flex: 1; flex-wrap: wrap; }
.bulk-actions:not([hidden]) + .list-title { display: none; }

.check { position: relative; display: inline-flex; width: 20px; height: 20px; flex: 0 0 auto; cursor: pointer; }
.check input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.check span { width: 18px; height: 18px; border-radius: 5px; border: 1.5px solid var(--faint); margin: 1px; display: inline-flex; align-items: center; justify-content: center; }
.check input:checked + span { background: var(--accent-strong); border-color: var(--accent-strong); }
.check input:checked + span::after { content: "✓"; color: #fff; font-size: 12px; font-weight: 700; }
.check input:indeterminate + span::after { content: "–"; color: var(--text); font-weight: 700; }

.list { flex: 1; overflow-y: auto; min-height: 0; }
.item {
  display: grid; grid-template-columns: 20px 1fr auto; gap: 2px 10px; padding: 10px 14px 10px 14px;
  border-bottom: 1px solid rgba(66,72,121,.55); cursor: pointer; position: relative;
}
.item:hover { background: var(--panel); }
.item.selected { background: var(--selected); }
.item .check { grid-row: 1 / span 2; align-self: center; }
.item .who { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.item .meta { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--faint); white-space: nowrap; }
.item .line2 { grid-column: 2 / span 2; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--faint); font-size: 13px; }
.item .line2 .subj { color: var(--muted); }
.item.unread .who, .item.unread .line2 .subj { color: var(--text); font-weight: 700; }
.item.unread::before { content: ""; position: absolute; left: 4px; top: 50%; width: 6px; height: 6px; margin-top: -3px; border-radius: 50%; background: var(--accent); }
.star-btn { background: none; border: 0; padding: 0 2px; cursor: pointer; color: var(--faint); font-size: 15px; line-height: 1; }
.star-btn.on { color: var(--star); }
.star-btn:hover { color: var(--star); }
.badge { display: inline-block; font-size: 11px; padding: 0 6px; border-radius: 6px; background: var(--panel-2); color: var(--muted); }
.badge.draft { color: var(--danger); background: transparent; padding: 0; font-weight: 600; }
.list-empty { padding: 48px 20px; text-align: center; color: var(--faint); }
.list-foot { padding: 8px 12px; display: flex; justify-content: center; }
.list-foot:empty { display: none; }

/* ───── 읽기 ───── */
.reader-pane { min-width: 0; min-height: 0; overflow-y: auto; background: var(--bg); }
.reader-empty { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--faint); }
.empty-mark { font-size: 44px; opacity: .35; }
.reader { padding: 14px 24px 40px; max-width: 980px; }
.reader-toolbar { display: flex; align-items: center; gap: 2px; flex-wrap: wrap; position: sticky; top: 0; background: var(--bg); padding: 4px 0 10px; z-index: 2; }
.reader-toolbar .spacer { flex: 1; }
.reader h1 { font-size: 21px; line-height: 1.35; margin: 6px 0 16px; font-weight: 700; word-break: break-word; }
.sender { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 14px; }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--accent-strong); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; flex: 0 0 auto; text-transform: uppercase; }
.sender-info { flex: 1; min-width: 0; }
.sender-name { font-weight: 700; }
.sender-addr { color: var(--muted); font-size: 13px; word-break: break-all; }
.recips { color: var(--faint); font-size: 12px; margin-top: 2px; word-break: break-word; }
.sender-date { color: var(--faint); font-size: 12px; white-space: nowrap; }
.notice { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 13px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; margin-bottom: 12px; color: var(--muted); }
.notice.warn { border-color: #8a4b55; color: #ffc2c9; }
.mail-frame-wrap { background: #fff; border-radius: var(--radius); overflow: hidden; }
.mail-frame { width: 100%; border: 0; display: block; min-height: 120px; background: #fff; }
.mail-text { white-space: pre-wrap; word-break: break-word; background: var(--panel); border-radius: var(--radius); padding: 16px 18px; margin: 0; font: 14px/1.65 var(--font); }
.atts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.att { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); text-decoration: none; color: var(--text); max-width: 280px; }
.att:hover { background: var(--hover); }
.att .att-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.att .att-size { color: var(--faint); font-size: 12px; white-space: nowrap; }
.att img { width: 36px; height: 36px; object-fit: cover; border-radius: 4px; }
.reply-bar { display: flex; gap: 8px; margin-top: 20px; flex-wrap: wrap; }
.confirm-inline { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--danger); }

/* ───── 작성 ───── */
.compose {
  position: fixed; right: 24px; bottom: 0; width: 640px; max-width: calc(100vw - 24px); height: min(720px, calc(100vh - 40px));
  background: var(--panel); border: 1px solid var(--line); border-bottom: 0; border-radius: 12px 12px 0 0;
  box-shadow: 0 10px 50px rgba(0,0,0,.45); display: flex; flex-direction: column; z-index: 50;
}
.compose.min { height: auto; }
.compose.min .compose-body, .compose.min .compose-foot { display: none; }
.compose-head { display: flex; align-items: center; padding: 8px 8px 8px 16px; background: var(--bg-deep); border-radius: 12px 12px 0 0; font-weight: 600; cursor: default; }
.compose-head span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.compose-head-actions { display: flex; }
.compose-body { flex: 1; display: flex; flex-direction: column; min-height: 0; overflow-y: auto; }
.compose-row { display: flex; align-items: center; gap: 8px; border-bottom: 1px solid var(--line); padding: 0 14px; min-height: 42px; }
.compose-row label { color: var(--faint); font-size: 13px; width: 72px; flex: 0 0 auto; }
.compose-row input, .compose-row select { flex: 1; min-width: 0; border: 0; background: transparent; outline: none; height: 40px; }
.compose-row select option { background: var(--panel); }
.addr-wrap { flex: 1; min-width: 0; display: flex; }
.addr-wrap input { width: 100%; }
.editor-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; padding: 6px 10px; border-bottom: 1px solid var(--line); flex: 0 0 auto; }
.editor-toolbar select { height: 30px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg-deep); padding: 0 6px; font-size: 12px; max-width: 120px; flex: 0 0 auto; }
.editor-toolbar select option { background: var(--panel); }
.tb-btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center; gap: 1px; flex: 0 0 auto;
  min-width: 30px; height: 30px; padding: 0 6px; border-radius: 6px; border: 0; background: transparent; color: var(--text);
  cursor: pointer; font-size: 14px; line-height: 1;
}
.tb-btn:hover { background: var(--hover); }
.tb-btn.on { background: var(--selected); }
.tb-sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; flex: 0 0 auto; }
.tb-color { flex-direction: column; gap: 2px; }
.tb-color input[type="color"] { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; border: 0; padding: 0; }
.tb-color-a { font-weight: 700; font-size: 13px; }
.tb-swatch { width: 16px; height: 3px; border-radius: 2px; background: #e03131; }
#tb-back-swatch { background: #ffec99; }
.tb-bar { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--line); background: var(--panel-2); flex-wrap: wrap; flex: 0 0 auto; }
.tb-bar input { flex: 1; min-width: 160px; height: 30px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg-deep); padding: 0 10px; outline: none; }
.tb-bar input:focus { border-color: var(--accent); }
.tb-bar-label { font-size: 12px; color: var(--muted); margin-right: 4px; }

/* 본문은 받는 사람 화면처럼 흰 바탕으로 편집 */
.compose-editor {
  flex: 1; min-height: 220px; outline: none; overflow-y: auto; background: #fff; color: #222;
  padding: 14px 16px; font: 14px/1.6 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  word-break: break-word; overflow-wrap: anywhere; cursor: text;
}
.compose-editor a { color: #1a5fd0; }
.compose-editor img { max-width: 100%; height: auto; cursor: pointer; }
.compose-editor img.img-selected { outline: 3px solid var(--accent-strong); outline-offset: 1px; }
.compose-editor blockquote { margin: 0 0 0 .8ex; border-left: 2px solid #ccc; padding-left: 1ex; color: #555; }
.compose-editor ul, .compose-editor ol { padding-left: 1.6em; margin: .3em 0; }
.compose-atts { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 8px; }
.compose-atts:empty { display: none; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--panel-2); border-radius: 6px; padding: 4px 6px 4px 10px; font-size: 12px; max-width: 260px; }
.chip .chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .chip-size { color: var(--faint); }
.chip button { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 0 4px; }
.compose .form-error { padding: 0 14px; }
.compose-foot { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-top: 1px solid var(--line); }
.compose-foot label.btn { cursor: pointer; }
.compose-status { font-size: 12px; color: var(--faint); }
.spacer { flex: 1; }
.suggest { position: absolute; left: 96px; right: 14px; background: var(--bg-deep); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 10px 30px rgba(0,0,0,.4); z-index: 5; overflow: hidden; }
.suggest div { padding: 8px 12px; cursor: pointer; font-size: 13px; }
.suggest div.active, .suggest div:hover { background: var(--selected); }
.suggest small { color: var(--faint); margin-left: 6px; }

/* ───── 토스트 ───── */
.toasts { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 100; align-items: center; pointer-events: none; }
.toast { background: #11132a; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: 0 8px 30px rgba(0,0,0,.4); font-size: 13px; display: flex; align-items: center; gap: 12px; pointer-events: auto; }
.toast.error { background: #6e2531; }
.toast button { background: none; border: 0; color: var(--accent); font-weight: 700; cursor: pointer; }

.mobile-only { display: none; }
.fab {
  position: fixed; right: 18px; bottom: max(18px, env(safe-area-inset-bottom)); width: 56px; height: 56px; border-radius: 18px;
  border: 0; background: var(--accent-strong); color: #fff; font-size: 22px; box-shadow: 0 8px 24px rgba(0,0,0,.4); cursor: pointer; z-index: 20;
  align-items: center; justify-content: center;
}
.back-btn { display: none; }

/* ───── 반응형 ───── */
@media (max-width: 1100px) {
  .app { grid-template-columns: 210px minmax(280px, 340px) 1fr; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .mobile-only { display: inline-flex; }
  .back-btn { display: inline-flex; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 270px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .app.drawer-open .sidebar { transform: none; box-shadow: 10px 0 40px rgba(0,0,0,.4); }
  .app.drawer-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 30; }
  .list-pane { border-right: 0; }
  .reader-pane { display: none; }
  .app.reading .list-pane { display: none; }
  .app.reading .reader-pane { display: block; }
  .reader { padding: 8px 14px 40px; }
  .reader h1 { font-size: 19px; }
  .sender { flex-wrap: wrap; }
  .sender-date { order: 3; width: 100%; padding-left: 52px; margin-top: -6px; }
  .compose { right: 0; left: 0; width: auto; max-width: none; height: 100%; border-radius: 0; top: 0; }
  .compose-head { border-radius: 0; padding-top: max(8px, env(safe-area-inset-top)); }
  .compose-foot { padding-bottom: max(10px, env(safe-area-inset-bottom)); }
  .compose.min { top: auto; }
  .editor-toolbar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .editor-toolbar::-webkit-scrollbar { display: none; }
}
