:root {
  --ink: #15201e;
  --muted: #64706d;
  --forest: #173b36;
  --forest-soft: #285c53;
  --paper: #f7f3e9;
  --panel: #fffdf7;
  --line: #ddd7c9;
  --sun: #e5ad58;
  --rose: #a6534f;
  --shadow: 0 22px 60px rgba(25, 44, 40, 0.13);
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--paper); }

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 8%, rgba(229, 173, 88, 0.2), transparent 28rem),
    radial-gradient(circle at 90% 96%, rgba(40, 92, 83, 0.16), transparent 32rem),
    var(--paper);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }

.shell {
  width: min(100% - 28px, 920px);
  min-height: 100vh;
  margin: 0 auto;
  padding: max(22px, env(safe-area-inset-top)) 0 max(28px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  padding: 6px 2px;
}

.brand-mark {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border-radius: 18px 8px 18px 8px;
  background: var(--forest);
  color: #fffaf0;
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
  letter-spacing: 0.05em;
  box-shadow: 0 8px 20px rgba(23, 59, 54, 0.2);
}

.brand-copy h1, .section-heading h2, .chat-header h2 {
  margin: 0;
  font-family: Georgia, "Times New Roman", "Noto Serif SC", serif;
  font-weight: 600;
}

.brand-copy h1 { font-size: clamp(1.25rem, 4vw, 1.65rem); }
.eyebrow, .step {
  margin: 0 0 3px;
  color: var(--forest-soft);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.13em;
  text-transform: uppercase;
}

.badge, .role-badge {
  min-height: 34px;
  display: inline-grid;
  place-items: center;
  padding: 0 12px;
  border: 1px solid rgba(23, 59, 54, 0.16);
  border-radius: 999px;
  background: rgba(255, 253, 247, 0.72);
  color: var(--forest);
  font-size: 0.78rem;
  font-weight: 750;
}

.notice {
  padding: 12px 16px;
  border-left: 4px solid var(--sun);
  border-radius: 4px 14px 14px 4px;
  background: rgba(255, 253, 247, 0.75);
  color: #4e5855;
  font-size: 0.88rem;
}

.notice.error { border-left-color: var(--rose); color: #7f3e3a; }
.notice.success { border-left-color: var(--forest-soft); color: var(--forest); }

.card, .chat-layout {
  border: 1px solid rgba(64, 74, 70, 0.13);
  border-radius: 26px;
  background: rgba(255, 253, 247, 0.94);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.auth-card {
  width: min(100%, 620px);
  margin: auto;
  padding: clamp(24px, 6vw, 44px);
}

.section-heading { margin-bottom: 28px; }
.section-heading h2 { font-size: clamp(1.7rem, 5vw, 2.35rem); }
.section-heading p:last-child { margin: 10px 0 0; color: var(--muted); line-height: 1.65; }

.form-stack { display: grid; gap: 18px; }
.form-stack label { display: grid; gap: 7px; color: #34413e; font-weight: 700; }
.form-stack small { color: var(--muted); font-size: 0.78rem; font-weight: 500; }

input, textarea {
  width: 100%;
  border: 1px solid #cfc8b8;
  border-radius: 14px;
  background: #fff;
  color: var(--ink);
  outline: none;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}

input { min-height: 50px; padding: 0 14px; }
textarea { min-height: 68px; padding: 13px 14px; resize: vertical; }
input:focus, textarea:focus { border-color: var(--forest-soft); box-shadow: 0 0 0 4px rgba(40, 92, 83, 0.12); }

.primary, .send {
  min-height: 50px;
  border: 0;
  border-radius: 14px;
  background: var(--forest);
  color: white;
  font-weight: 800;
  box-shadow: 0 10px 22px rgba(23, 59, 54, 0.2);
}

.primary:hover, .send:hover { background: #214e47; }
.primary:disabled, .send:disabled { cursor: wait; opacity: 0.6; }

.role-picker {
  margin: 0;
  padding: 0;
  border: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.role-picker legend { margin-bottom: 8px; color: #34413e; font-weight: 700; }
.role-option { position: relative; }
.role-option input { position: absolute; opacity: 0; pointer-events: none; }
.role-option span {
  min-height: 72px;
  display: grid;
  align-content: center;
  gap: 4px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: #fff;
}
.role-option small { display: block; }
.role-option input:checked + span { border-color: var(--forest); background: #edf4f1; box-shadow: inset 0 0 0 1px var(--forest); }
.role-option input:focus-visible + span { outline: 3px solid rgba(229, 173, 88, 0.7); outline-offset: 2px; }
.debug-remember { display: flex !important; align-items: flex-start; grid-template-columns: auto 1fr; gap: 10px !important; padding: 12px; border: 1px dashed var(--line); border-radius: 14px; background: rgba(255,255,255,.5); }
.debug-remember input { width: auto; margin-top: 4px; }
.debug-remember strong, .debug-remember small { display: block; }
.debug-unlock-panel { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 12px; border-radius: 14px; background: rgba(27,79,71,.08); }
.debug-unlock-panel small { flex-basis: 100%; color: var(--muted); }

.chat-layout { min-height: min(680px, calc(100vh - 170px)); display: grid; grid-template-rows: auto auto auto 1fr auto; }
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px clamp(18px, 4vw, 28px);
  border-bottom: 1px solid var(--line);
  background: linear-gradient(135deg, rgba(237, 244, 241, 0.9), rgba(255, 253, 247, 0.94));
}
.chat-header { grid-row: 1; }
.chat-header h2 { font-size: clamp(1.35rem, 4vw, 1.8rem); }
.chat-actions { display: flex; align-items: center; gap: 9px; }
.quiet {
  min-height: 40px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: white;
  color: var(--ink);
  font-weight: 700;
}

.key-panel-toggle { justify-self: end; min-height: 34px; margin: 7px 14px 0; font-size: 0.76rem; }
.key-panel-toggle { grid-row: 2; }
.e2ee-panel { grid-row: 3; margin: 10px 18px; padding: 14px; border: 1px solid #cdd8cf; border-radius: 14px; background: #f7fbf8; }
.e2ee-panel.is-collapsed { display: none; }
.e2ee-panel h3, .e2ee-panel h4 { margin: 2px 0 8px; }
.key-state, .key-hint { margin: 7px 0; color: var(--muted); font-size: 0.82rem; line-height: 1.45; }
.automatic-pairing > button { width: 100%; }
.pairing-code { margin: 10px 0; padding: 12px; border-radius: 12px; background: #e8f4f7; text-align: center; font-size: 1.05rem; font-weight: 800; letter-spacing: .08em; }
.pairing-requests { display: grid; gap: 8px; }
.pairing-request { padding: 10px; border: 1px solid #cdd8cf; border-radius: 10px; background: #fff; }
.pairing-request p { margin: 0 0 8px; }
.pairing-request button { width: 100%; min-height: 42px; }
.recovery-pairing { margin-top: 12px; }
.recovery-pairing summary { cursor: pointer; font-weight: 750; }
.recovery-pairing label { display: grid; gap: 6px; margin-top: 8px; font-size: .82rem; font-weight: 700; }
.key-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.composer-tools-toggle { margin-bottom: 9px; }

.timeline {
  grid-row: 4;
  min-height: 300px;
  padding: 24px clamp(14px, 4vw, 28px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 13px;
  background-image: linear-gradient(rgba(255,255,255,0.35), rgba(255,255,255,0.35));
}
.empty-state { margin: auto; display: grid; justify-items: center; gap: 7px; color: var(--muted); text-align: center; }
.empty-icon { width: 48px; height: 48px; display: grid; place-items: center; border-radius: 50%; background: #edf4f1; color: var(--forest); font-size: 1.3rem; }

.message { max-width: min(78%, 560px); display: grid; gap: 5px; }
.message.own { align-self: flex-end; justify-items: end; }
.message.other { align-self: flex-start; justify-items: start; }
.bubble { padding: 11px 14px; border-radius: 18px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere; }
.own .bubble { border-bottom-right-radius: 5px; background: var(--forest); color: white; }
.other .bubble { border-bottom-left-radius: 5px; background: #efe9dc; color: var(--ink); }
.message-meta { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 0.72rem; }
.delete-message { border: 0; padding: 0; background: transparent; color: var(--rose); font-size: 0.72rem; text-decoration: underline; }

.composer { grid-row: 5; padding: 14px clamp(14px, 4vw, 24px) 17px; border-top: 1px solid var(--line); background: #fffdf7; }
.attachment-toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.attach-button, .media-load {
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #f8f5ed;
  color: var(--forest);
  font-weight: 750;
}
.attach-button:hover, .media-load:hover { border-color: var(--forest-soft); background: #edf4f1; }
.usb-audio-button, .usb-play { border-color: rgba(40, 92, 83, 0.42); background: #edf4f1; }
.attach-button:disabled, .media-load:disabled { cursor: wait; opacity: 0.58; }
.attachment-preview {
  margin: 0 0 10px;
  padding: 10px;
  border: 1px dashed #b8b09f;
  border-radius: 14px;
  background: #fbf8f1;
}
.attachment-preview img, .attachment-preview video { display: block; max-width: min(100%, 360px); max-height: 220px; border-radius: 10px; }
.attachment-preview audio { width: min(100%, 420px); }
.media-duration { margin-top: 4px; color: var(--ink); font-size: 0.82rem; font-variant-numeric: tabular-nums; }
.preview-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 8px; color: var(--muted); font-size: 0.78rem; }
.quiet.danger { color: var(--rose); }
.attachment-preview progress { width: 100%; height: 8px; margin-top: 8px; accent-color: var(--forest); }
.message-text + .message-media { margin-top: 9px; }
.message-media img, .message-media video { display: block; width: min(100%, 440px); max-height: 360px; object-fit: contain; border-radius: 12px; background: rgba(0,0,0,0.08); }
.message-media audio { display: block; width: min(100%, 360px); }
.audio-playback { display: grid; gap: 7px; justify-items: start; }
.audio-playback .usb-play { min-height: 34px; font-size: 0.76rem; }
.own .media-load { background: rgba(255,255,255,0.94); }
.composer-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 8px; }
.composer-row span { color: var(--muted); font-size: 0.75rem; }
.send { min-width: 108px; }

footer { margin-top: auto; display: flex; justify-content: space-between; gap: 14px; padding: 0 4px; color: #6f7875; font-size: 0.72rem; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

@media (max-width: 620px) {
  body.message-mode { height: 100vh; height: 100dvh; overflow: hidden; overscroll-behavior: none; }
  body.message-mode .shell { width: 100%; height: 100vh; height: 100dvh; min-height: 0; padding: 0; gap: 0; overflow: hidden; }
  body.message-mode .shell > .topbar, body.message-mode .shell > .notice, body.message-mode .shell > footer { display: none; }
  .shell { width: min(100% - 16px, 920px); gap: 12px; }
  .topbar { gap: 10px; }
  .brand-mark { width: 44px; height: 44px; border-radius: 15px 7px 15px 7px; }
  .eyebrow { display: none; }
  .badge { padding: 0 9px; font-size: 0.7rem; }
  .auth-card { border-radius: 20px; }
  .role-picker { grid-template-columns: 1fr; }
  .chat-layout { min-height: calc(100vh - 132px); border-radius: 18px; }
  body.message-mode .chat-layout { width: 100%; height: 100vh; height: 100dvh; min-height: 0; border: 0; border-radius: 0; box-shadow: none; overflow: hidden; grid-template-rows: auto auto auto minmax(0, 1fr) auto; }
  .chat-header { align-items: center; min-height: 58px; padding: max(8px, env(safe-area-inset-top)) 10px 8px; }
  .chat-header h2 { font-size: 1.08rem; }
  .chat-actions { align-items: flex-end; flex-direction: row; gap: 6px; }
  .chat-actions .quiet:not(#logoutButton), .chat-header .step { display: none; }
  .role-badge, #logoutButton { min-height: 34px; padding: 5px 8px; font-size: .76rem; }
  .key-panel-toggle { justify-self: end; min-height: 30px; margin: 3px 8px; padding: 4px 8px; font-size: .72rem; }
  .e2ee-panel { margin: 0 8px 6px; }
  .e2ee-panel:not(.is-collapsed) { position: fixed; z-index: 30; top: max(64px, calc(env(safe-area-inset-top) + 54px)); right: 8px; bottom: max(84px, calc(env(safe-area-inset-bottom) + 74px)); left: 8px; margin: 0; overflow-y: auto; overscroll-behavior: contain; box-shadow: 0 18px 40px rgba(10,42,50,.24), 0 0 0 100vmax rgba(245,250,248,.72); }
  .timeline { min-height: 0; max-height: none; gap: 8px; padding: 10px; overflow-x: hidden; overflow-y: auto; overscroll-behavior-y: contain; touch-action: pan-y; -webkit-overflow-scrolling: touch; }
  .message { max-width: 92%; }
  .bubble { padding: 9px 11px; }
  .composer { display: grid; grid-template-columns: 44px minmax(0, 1fr) auto; grid-template-areas: "tools message send" "attachments attachments attachments" "preview preview preview" "count count count"; gap: 6px 8px; max-height: 52dvh; overflow-y: auto; padding: 8px 10px max(8px, env(safe-area-inset-bottom)); }
  .composer-tools-toggle { grid-area: tools; width: 44px; min-width: 44px; min-height: 44px; margin: 0; padding: 0; font-size: 0; }
  .composer-tools-toggle::after { content: "+"; font-size: 1.45rem; line-height: 1; }
  .composer-tools-toggle[aria-expanded="true"]::after { content: "\00d7"; }
  .composer > textarea { grid-area: message; height: 44px; min-height: 44px; max-height: 96px; padding: 10px 11px; resize: none; }
  .composer-row { display: contents; }
  .composer-row span { grid-area: count; min-height: 0; margin: 0; }
  .composer-row button { grid-area: send; min-width: 68px; min-height: 44px; padding: 8px 12px; }
  .attachment-toolbar { grid-area: attachments; gap: 6px; margin: 0; }
  .attachment-preview { grid-area: preview; margin: 0; }
  .composer.tools-collapsed .attachment-toolbar { display: none; }
  .attach-button { padding: 0 9px; font-size: 0.78rem; }
  .key-actions button { flex: 1 1 100%; }
  footer { padding-bottom: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; }
}
