:root {
  --paper: #f2efe7;
  --card: #fbfaf6;
  --ink: #1f1e1c;
  --muted: #6d695f;
  --line: #e0dbcf;
  --accent: #9e3b2c;
  --accent-ink: #fff;
  --eink: #e8e6de;
  --radius: 14px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 6px 24px rgba(40, 30, 10, .06);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.5 var(--sans);
  min-height: 100vh;
}
[hidden] { display: none !important; }
h1, h2 { font-family: var(--serif); font-weight: 600; letter-spacing: -.01em; margin: 0 0 .4em; }
h1 { font-size: 1.7rem; line-height: 1.2; }
h2 { font-size: 1.15rem; margin-top: .2em; }
a { color: var(--accent); }
small, .muted { color: var(--muted); font-size: .85rem; }
label small { display: block; font-weight: 400; margin-top: .35rem; line-height: 1.4; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1.5rem;
  padding: .7rem clamp(1rem, 3vw, 2rem);
  padding-top: max(.7rem, env(safe-area-inset-top));
  background: rgba(242, 239, 231, .9);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .55rem; color: var(--ink); text-decoration: none; font: 600 1.15rem var(--serif); }
.brand svg { width: 28px; height: 28px; }
.tabs { display: flex; gap: .25rem; flex: 1; overflow-x: auto; scrollbar-width: none; }
.tabs button {
  border: 0; background: none; font: inherit; color: var(--muted); cursor: pointer;
  padding: .45rem .85rem; border-radius: 999px; white-space: nowrap;
}
.tabs button:hover { color: var(--ink); background: rgba(0, 0, 0, .04); }
.tabs button.active { color: var(--ink); background: #e6e1d4; font-weight: 600; }

main { padding: clamp(1rem, 3vw, 2rem); max-width: 1400px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } }

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: clamp(1rem, 2.5vw, 1.6rem);
  box-shadow: var(--shadow);
}

/* ---------- forms ---------- */
.label, label { display: block; font-weight: 600; font-size: .88rem; margin: 1rem 0 .4rem; }
label > select, label > input:not([type=checkbox]), label > .seg { margin-top: .4rem; font-weight: 400; }
select, input[type=text], input[type=password], input[type=time], input:not([type]), textarea {
  width: 100%; font: inherit; color: var(--ink);
  background: #fff; border: 1px solid var(--line); border-radius: 10px;
  padding: .6rem .75rem;
}
textarea { resize: vertical; min-height: 110px; font-size: 1.05rem; line-height: 1.45; padding-right: 3.2rem; }
select:focus, input:focus, textarea:focus { outline: 2px solid rgba(158, 59, 44, .35); outline-offset: 1px; border-color: var(--accent); }
input[type=range] { width: 100%; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: .55rem; font-weight: 500; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.hint { color: var(--muted); margin: 0 0 1rem; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .5rem;
  font: 600 .95rem var(--sans); color: var(--ink);
  background: #fff; border: 1px solid var(--line); border-radius: 999px;
  padding: .65rem 1.2rem; cursor: pointer; transition: transform .08s, background .15s, box-shadow .15s;
}
.btn:hover { background: #f6f4ee; }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn.primary:hover { background: #33312d; }
.btn.big { width: 100%; padding: .9rem 1.2rem; font-size: 1.05rem; margin-top: 1.2rem; }
.btn.small { padding: .4rem .8rem; font-size: .82rem; }
.btn.ghost { background: transparent; }
.btn svg { width: 18px; height: 18px; }
.link { background: none; border: 0; color: var(--accent); font: 600 .9rem var(--sans); cursor: pointer; padding: .3rem 0; }
.link.small { font-size: .85rem; }
.actions { display: flex; gap: .7rem; justify-content: flex-end; margin-top: 1.2rem; flex-wrap: wrap; }

.seg { display: inline-flex; background: #ece8dd; border-radius: 999px; padding: 3px; gap: 2px; }
.seg.wide { display: flex; }
.seg.wide button { flex: 1; }
.seg button {
  border: 0; background: none; font: 500 .86rem var(--sans); color: var(--muted);
  padding: .42rem .9rem; border-radius: 999px; cursor: pointer;
}
.seg button.on { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .1); font-weight: 600; }

.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chips button {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  font: 500 .85rem var(--sans); padding: .38rem .8rem; border-radius: 999px; cursor: pointer;
}
.chips button:hover { border-color: #c9c2b0; }
.chips button.on { background: var(--ink); color: #fff; border-color: var(--ink); }

.divider { display: flex; align-items: center; gap: .8rem; color: var(--muted); font-size: .85rem; margin: 1.2rem 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.file-btn { width: 100%; margin: 0; font-weight: 600; }

/* ---------- create ---------- */
.create-grid { display: grid; grid-template-columns: minmax(300px, 400px) 1fr; gap: clamp(1rem, 2.5vw, 2rem); align-items: start; }
.prompt-wrap { position: relative; }
.mic {
  position: absolute; right: .55rem; top: .55rem;
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--paper); color: var(--ink); cursor: pointer;
  display: grid; place-items: center;
}
.mic svg { width: 20px; height: 20px; }
.mic.listening { background: var(--accent); color: #fff; border-color: var(--accent); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 8px rgba(158, 59, 44, .15); } }

.preview { position: sticky; top: 5rem; }
.tv { background: #1a1a1a; border-radius: 12px; padding: clamp(6px, 1vw, 12px); box-shadow: 0 18px 40px rgba(30, 20, 0, .18); }
.tv-foot { height: 6px; }
.tv-screen {
  position: relative; aspect-ratio: 16 / 9; background: var(--eink); overflow: hidden; border-radius: 3px;
}
.tv-screen canvas, .tv-screen img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; }
.tv-screen canvas.blank { visibility: hidden; }
.paper-overlay {
  position: absolute; inset: 0; pointer-events: none; mix-blend-mode: multiply; opacity: .5;
  background-repeat: repeat;
}
.paper-overlay::after {
  content: ""; position: absolute; inset: 0;
  background: radial-gradient(ellipse at 50% 40%, rgba(255, 255, 255, 0) 55%, rgba(80, 70, 50, .10) 100%);
}
.screen-empty {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: .6rem;
  color: #8a8579; font-family: var(--serif); font-size: 1.05rem; text-align: center; padding: 2rem;
}
.screen-empty svg { width: 64px; height: 64px; }
.busy {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 1rem;
  background: rgba(232, 230, 222, .86); color: var(--ink); font: 1.1rem var(--serif);
}
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 3px solid rgba(0, 0, 0, .12); border-top-color: var(--ink); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.tv-screen:fullscreen { background: var(--eink); border-radius: 0; cursor: none; }
.tv-screen:-webkit-full-screen { background: var(--eink); width: 100%; height: 100%; }

.preview-bar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin: .9rem 0 .2rem; flex-wrap: wrap; }
.preview-title { display: flex; flex-direction: column; min-width: 0; }
.preview-title strong { font-family: var(--serif); font-size: 1.1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview-title span { color: var(--muted); font-size: .85rem; }
.preview-tools { display: flex; gap: .4rem; }

.controls { margin-top: .8rem; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: .4rem 1.2rem 1rem; }
.controls summary { cursor: pointer; font: 600 1rem var(--serif); padding: .6rem 0; }
.control-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); column-gap: 1.4rem; }
.control-grid label { margin-top: .7rem; }

/* ---------- library ---------- */
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1.2rem; flex-wrap: wrap; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 1.1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); display: flex; flex-direction: column; position: relative; }
.card .thumb { aspect-ratio: 16 / 9; background: var(--eink); cursor: pointer; position: relative; overflow: hidden; flex: none; }
.card .thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.card .meta { padding: .7rem .9rem .2rem; min-width: 0; }
.card .title { font: 600 .98rem var(--serif); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .sub { color: var(--muted); font-size: .8rem; }
.card .card-actions { display: flex; gap: .3rem; padding: .5rem .6rem .7rem; align-items: center; }
.card .card-actions .spacer { flex: 1; }
.icon-btn { border: 0; background: none; cursor: pointer; width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; color: var(--muted); }
.icon-btn:hover { background: rgba(0, 0, 0, .05); color: var(--ink); }
.icon-btn svg { width: 19px; height: 19px; }
.icon-btn.fav.on { color: #c08a12; }
.badge { position: absolute; top: .6rem; left: .6rem; background: var(--ink); color: #fff; font: 600 .72rem var(--sans); padding: .25rem .6rem; border-radius: 999px; letter-spacing: .02em; }
.badge.light { background: rgba(255, 255, 255, .9); color: var(--ink); }
.lib-count { margin: -.2rem 0 0; }
.empty-note { color: var(--muted); text-align: center; padding: 3rem 1rem; font-family: var(--serif); font-size: 1.1rem; }

/* ---------- frame + devices ---------- */
.frame-grid { display: grid; grid-template-columns: minmax(280px, 1fr) minmax(320px, 1.3fr); gap: 1.4rem; align-items: start; }
.now img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 8px; border: 6px solid #1a1a1a; background: var(--eink); }
.now .title { font: 600 1.05rem var(--serif); margin-top: .6rem; }
.rotate-opts { display: grid; grid-template-columns: repeat(3, 1fr); gap: .8rem; }
.rotate-opts.off { display: none; }
#frame-form h2 { margin-top: 1.4rem; }
#frame-form h2:first-child { margin-top: .2rem; }
.steps { padding-left: 1.2rem; color: var(--muted); }
.steps li { margin: .3rem 0; }
.code-input { font: 600 1.6rem ui-monospace, Menlo, monospace !important; letter-spacing: .25em; text-transform: uppercase; text-align: center; }
.frames { list-style: none; padding: 0; margin: 0; }
.frames li { display: flex; align-items: center; gap: .8rem; padding: .8rem 0; border-bottom: 1px solid var(--line); }
.frames li:last-child { border-bottom: 0; }
.frames .dot { width: 10px; height: 10px; border-radius: 50%; background: #bbb; flex: none; }
.frames .dot.on { background: #3b8a4f; box-shadow: 0 0 0 4px rgba(59, 138, 79, .15); }
.frames .info { flex: 1; min-width: 0; }
.frames .name { font-weight: 600; }
.frames .seen { color: var(--muted); font-size: .82rem; }

/* ---------- login ---------- */
.overlay { position: fixed; inset: 0; z-index: 50; background: var(--paper); display: grid; place-items: center; padding: 1.5rem; }
.login-card { width: min(380px, 100%); text-align: center; }
.login-card svg { width: 56px; height: 56px; }
.login-card p { color: var(--muted); }
.login-card input { margin-top: 1rem; text-align: center; font-size: 1.05rem; }
.error { color: var(--accent) !important; min-height: 1.5em; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: max(1.5rem, env(safe-area-inset-bottom)); transform: translate(-50%, 150%);
  background: var(--ink); color: #fff; padding: .75rem 1.2rem; border-radius: 12px; font-weight: 500;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .2); transition: transform .3s ease; z-index: 60; max-width: calc(100vw - 2rem);
}
.toast.show { transform: translate(-50%, 0); }
.toast.error { background: var(--accent); }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .create-grid, .frame-grid { grid-template-columns: 1fr; }
  .preview { position: static; order: -1; }
  .rotate-opts { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .topbar { gap: .8rem; flex-wrap: wrap; }
  .brand span { display: none; }
  .tabs { order: 3; flex-basis: 100%; }
  #logout { margin-left: auto; }
  h1 { font-size: 1.45rem; }
}
