:root {
  --desk: #e9dcc8;
  --desk-2: #dccab0;
  --paper: #fffdf7;
  --ink: #2a2622;
  --muted: #7d7368;
  --line: #e7dfd2;
  --bar: rgba(255, 252, 246, 0.92);
  --bar-border: rgba(80, 60, 40, 0.12);
  --accent: #b8452f;
  --day: #d32f2f;
  --day-bright: #e53935;
  --radius: 18px;
  --font-head: "Mitr", "Sarabun", system-ui, sans-serif;
  --font-body: "Sarabun", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --desk: #2b2622;
    --desk-2: #221e1b;
    --bar: rgba(40, 35, 31, 0.92);
    --bar-border: rgba(255, 255, 255, 0.08);
    --text-on-desk: #efe6da;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: radial-gradient(120% 90% at 50% 0%, var(--desk), var(--desk-2));
  color: var(--ink);
  font-family: var(--font-body);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px 4px;
  color: var(--text-on-desk, var(--ink));
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand b { font-family: var(--font-head); font-weight: 600; font-size: 20px; display: block; line-height: 1.1; }
.brand small { font-size: 12px; opacity: .7; display: block; }
.actions { display: flex; gap: 6px; align-items: center; }
.btn {
  border: 0; border-radius: 999px; padding: 8px 16px;
  background: var(--accent); color: #fff; font-family: var(--font-head); font-size: 15px;
}
.btn.ghost { background: transparent; color: inherit; box-shadow: inset 0 0 0 1.5px currentColor; opacity: .8; }
.icon-btn {
  width: 40px; height: 40px; border: 0; border-radius: 50%;
  background: transparent; display: grid; place-items: center;
}
.icon-btn:hover, .icon-btn:focus-visible { background: rgba(0,0,0,.07); }

/* ---------- desk + page ---------- */
#stage {
  min-height: 0;
  display: flex; justify-content: center;
  padding: 18px 16px 10px;
}
.desk {
  position: relative;
  width: 100%; max-width: 760px;
  display: flex; flex-direction: column;
  min-height: 0;
  perspective: 1400px;
}
.rings {
  position: absolute; top: -12px; left: 8%; right: 8%;
  display: flex; justify-content: space-between; z-index: 3; pointer-events: none;
}
.rings i {
  width: 9px; height: 26px; border-radius: 6px;
  background: linear-gradient(90deg, #6b6460, #d8d3cc 45%, #8a837d);
  box-shadow: 0 2px 2px rgba(0,0,0,.25);
}
.page {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  background: var(--paper);
  border-radius: 10px 10px var(--radius) var(--radius);
  box-shadow:
    0 1px 0 #f3ede2,
    0 3px 0 -1px #efe7da, 0 4px 0 -1px #d8cfc0,
    0 7px 0 -2px #ece4d6, 0 8px 0 -2px #d2c8b8,
    0 18px 30px rgba(60, 40, 20, .25);
  touch-action: none;
  transform-origin: 50% 0;
  overflow: hidden;
}
.page.flip-next { animation: flipNext .38s cubic-bezier(.2,.8,.3,1); }
.page.flip-prev { animation: flipPrev .38s cubic-bezier(.2,.8,.3,1); }
@keyframes flipNext { from { transform: rotateX(28deg) translateY(-10px); opacity: .2; } }
@keyframes flipPrev { from { transform: translateY(26px); opacity: .2; } }

.date-head {
  flex: none;
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 8px 6px;
  border-top: 10px solid var(--day-bright);
  border-radius: 10px 10px 0 0;
  background: linear-gradient(var(--day-tint, #fff4f2), var(--paper));
}
.dh-center { text-align: center; flex: 1; min-width: 0; }
.dh-month { font-family: var(--font-head); font-size: 17px; color: var(--muted); }
.dh-day {
  font-family: var(--font-head); font-weight: 600; color: var(--day);
  font-size: clamp(56px, 13vh, 120px); line-height: 1; margin: 2px 0;
  font-variant-numeric: tabular-nums;
}
.dh-weekday { font-family: var(--font-head); font-size: 20px; color: var(--day); }
.dh-holiday { font-size: 14px; min-height: 20px; color: var(--muted); padding: 0 4px; }
.dh-holiday.off { color: #c62828; font-weight: 600; }
.nav {
  width: 48px; height: 48px; border-radius: 50%; border: 0; flex: none;
  background: transparent; color: var(--muted); display: grid; place-items: center;
}
.nav:hover { background: rgba(0,0,0,.05); }
.nav svg { width: 28px; height: 28px; }

.note-wrap {
  position: relative;
  flex: 1; min-height: 0;
  display: flex;
  padding: 4px 12px 12px;
}
.note { position: relative; flex: 1; min-width: 0; touch-action: none; border-radius: 8px; overflow: hidden; }

.zoom-ctl {
  position: absolute; right: 8px; bottom: 8px; z-index: 2;
  display: flex; align-items: center;
  background: rgba(255, 253, 247, .92); border: 1px solid var(--line); border-radius: 999px;
  box-shadow: 0 2px 8px rgba(60, 40, 20, .15);
}
.zoom-ctl button {
  border: 0; background: transparent; height: 34px; min-width: 34px; padding: 0 6px;
  display: grid; place-items: center; font-size: 13px; font-variant-numeric: tabular-nums; color: var(--ink);
}
.zoom-ctl svg { width: 18px; height: 18px; }
.extend-btn {
  position: absolute; z-index: 2; transform: translateX(-50%);
  border: 1.5px dashed #c9b89c; background: rgba(255, 253, 247, .95); color: var(--muted);
  border-radius: 999px; padding: 8px 18px; font-family: var(--font-head); font-size: 15px; white-space: nowrap;
}
.extend-btn:not(:disabled):hover { border-color: var(--accent); color: var(--accent); }
.extend-btn:disabled { opacity: .6; cursor: default; }
.vscroll {
  position: absolute; right: 3px; top: 6px; bottom: 52px; width: 4px; z-index: 1; pointer-events: none;
}
.vscroll i { position: absolute; left: 0; right: 0; border-radius: 4px; background: rgba(80, 60, 40, .3); }
.note canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#live.hl { opacity: .4; mix-blend-mode: multiply; }

.text-panel {
  position: absolute; left: 12px; right: 12px; bottom: 12px;
  background: rgba(255, 253, 247, .97);
  border: 1.5px solid var(--line); border-radius: 14px;
  box-shadow: 0 8px 22px rgba(60,40,20,.15);
  padding: 8px;
}
.text-panel textarea {
  width: 100%; height: min(28vh, 220px); resize: none; border: 0; outline: none;
  background: transparent; font: 18px/1.6 var(--font-body); color: var(--ink);
  white-space: pre-wrap; overflow-wrap: anywhere; line-break: auto;
  user-select: text; -webkit-user-select: text;
}

/* landscape tablets: date on the left, paper on the right */
@media (min-width: 760px) and (min-aspect-ratio: 5/4) {
  .desk { max-width: 1180px; }
  .page { flex-direction: row; border-radius: 10px var(--radius) var(--radius) var(--radius); }
  .date-head {
    width: 32%;
    display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr auto;
    justify-items: center; padding: 20px 12px;
    border-top: 0; border-left: 10px solid var(--day-bright);
    border-radius: 10px 0 0 var(--radius);
    background: linear-gradient(160deg, var(--day-tint, #fff4f2), var(--paper) 70%);
    border-right: 1px dashed var(--line);
  }
  .date-head .dh-center { grid-row: 1; grid-column: 1 / 3; align-self: center; }
  #prevDay { grid-row: 2; grid-column: 1; }
  #nextDay { grid-row: 2; grid-column: 2; }
  .dh-day { font-size: clamp(90px, 22vh, 200px); }
  .dh-weekday { font-size: 26px; }
  .dh-month { font-size: 20px; }
}

/* ---------- toolbar ---------- */
.toolbar {
  display: flex; align-items: center; gap: 2px;
  margin: 0 auto calc(10px + env(safe-area-inset-bottom));
  padding: 6px 8px;
  max-width: calc(100vw - 16px);
  overflow-x: auto; scrollbar-width: none;
  background: var(--bar); color: var(--text-on-desk, var(--ink));
  border: 1px solid var(--bar-border); border-radius: 999px;
  box-shadow: 0 8px 24px rgba(40, 25, 10, .18);
  backdrop-filter: blur(10px);
}
.toolbar::-webkit-scrollbar { display: none; }
.tool {
  flex: none; width: 42px; height: 42px; border: 0; border-radius: 50%;
  background: transparent; display: grid; place-items: center;
}
.tool:disabled { opacity: .3; cursor: default; }
.tool.active { background: var(--accent); color: #fff; }
.tool.has-text::after {
  content: ""; position: absolute; width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); transform: translate(12px, -12px);
}
.tool.size span { display: block; border-radius: 50%; background: currentColor; }
.sep { flex: none; width: 1px; height: 26px; background: var(--bar-border); margin: 0 4px; }
.swatches { display: flex; gap: 2px; }
.swatch {
  flex: none; width: 34px; height: 34px; border-radius: 50%; border: 0; padding: 0;
  display: grid; place-items: center; background: transparent;
}
.swatch i { width: 22px; height: 22px; border-radius: 50%; box-shadow: inset 0 0 0 1px rgba(0,0,0,.15); }
.swatch.active i { box-shadow: 0 0 0 2.5px var(--bar), 0 0 0 4.5px currentColor; }

@media (max-width: 420px) {
  .toolbar { gap: 0; padding: 4px 6px; }
  .tool { width: 30px; height: 36px; }
  .swatch { width: 24px; height: 28px; }
  .swatch i { width: 18px; height: 18px; }
  .sep { display: none; }
  svg { width: 20px; height: 20px; }
  .brand small { display: none; }
  #stage { padding-top: 16px; }
}

/* ---------- sheets (dialogs) ---------- */
dialog.sheet {
  border: 0; padding: 18px; border-radius: 22px;
  width: min(440px, calc(100vw - 24px));
  background: var(--paper); color: var(--ink);
  box-shadow: 0 24px 60px rgba(30, 20, 10, .35);
  font-family: var(--font-body);
}
dialog::backdrop { background: rgba(30, 20, 10, .35); backdrop-filter: blur(2px); }
dialog h2 { font-family: var(--font-head); font-weight: 500; font-size: 20px; margin: 0 0 10px; text-align: center; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h2 { margin: 0; }
.sheet-foot { display: flex; align-items: center; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.legend { margin-right: auto; font-size: 14px; color: var(--muted); display: flex; align-items: center; gap: 6px; }

.month-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-top: 12px; }
.month-grid .wd { text-align: center; font-size: 13px; color: var(--muted); padding: 4px 0; font-family: var(--font-head); }
.month-grid .wd.sun { color: #d32f2f; }
.month-grid button {
  aspect-ratio: 1; border: 0; border-radius: 12px; background: transparent;
  font-family: var(--font-head); font-size: 17px; position: relative;
  display: grid; place-items: center;
}
.month-grid button:hover { background: rgba(0,0,0,.05); }
.month-grid button.sun, .month-grid button.off { color: #d32f2f; }
.month-grid button.today { box-shadow: inset 0 0 0 2px var(--accent); }
.month-grid button.sel { background: var(--accent); color: #fff; }
.dot, .month-grid button.has::after {
  content: ""; display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--accent);
}
.month-grid button.has::after { position: absolute; bottom: 6px; left: calc(50% - 3px); }
.month-grid button.sel.has::after { background: #fff; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 2px; border-bottom: 1px solid var(--line); }
.row b { display: block; font-weight: 600; }
.row small { display: block; color: var(--muted); font-size: 13px; }
.switch input { width: 46px; height: 26px; accent-color: var(--accent); flex: none; }
.seg { display: flex; background: #f1ebe0; border-radius: 999px; padding: 3px; }
.seg button { border: 0; background: transparent; border-radius: 999px; padding: 5px 10px; font-size: 14px; }
.seg button.active { background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.menu-list { display: flex; flex-direction: column; margin-top: 8px; }
.menu-list button { text-align: left; border: 0; background: transparent; padding: 12px 4px; font-size: 16px; border-bottom: 1px solid var(--line); }
.menu-list button:hover { background: rgba(0,0,0,.03); }
.menu-list .danger { color: #c62828; }
.about { font-size: 12px; color: var(--muted); text-align: center; margin: 12px 0 0; }

.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #2a2622; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 15px; transition: .25s; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- account ---------- */
.account-btn { position: relative; padding: 0; }
.account-btn #avatar { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; overflow: hidden; }
.account-btn img, .account img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.avatar-fallback {
  display: grid; place-items: center; width: 100%; height: 100%; border-radius: 50%;
  background: var(--accent); color: #fff; font-family: var(--font-head); font-size: 16px;
}
.sync-dot {
  position: absolute; right: 3px; bottom: 3px; width: 10px; height: 10px; border-radius: 50%;
  border: 2px solid var(--desk); background: transparent;
}
.sync-dot.ok { background: #2e9d57; }
.sync-dot.syncing { background: #f4c430; }
.sync-dot.offline { background: #9e9e9e; }
.sync-dot.error, .sync-dot.expired { background: #e53935; }

.account { padding-bottom: 12px; margin-bottom: 8px; border-bottom: 1px solid var(--line); }
.acc-row { display: flex; align-items: center; gap: 12px; }
.acc-row > img, .acc-row > .avatar-fallback { width: 48px; height: 48px; flex: none; font-size: 22px; }
.acc-info { min-width: 0; }
.acc-info b { display: block; font-family: var(--font-head); font-weight: 500; font-size: 17px; }
.acc-info small { display: block; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; }
.acc-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; }
.acc-actions .btn { text-decoration: none; font-size: 14px; padding: 7px 14px; }
.acc-note { margin: 0 0 10px; color: var(--muted); font-size: 14px; text-align: center; }

.google-btn {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  padding: 11px 18px; border-radius: 999px; background: #fff; color: #1f1f1f;
  border: 1px solid #c9c2b8; text-decoration: none; font-weight: 600; font-size: 16px;
  box-shadow: 0 1px 2px rgba(0,0,0,.08);
}
.google-btn svg { width: 20px; height: 20px; stroke: none; }
.dev-login { display: block; text-align: center; margin-top: 8px; font-size: 13px; color: var(--muted); }

dialog.welcome { text-align: center; display: none; }
dialog.welcome[open] { display: flex; flex-direction: column; align-items: stretch; gap: 12px; }
dialog.welcome img { align-self: center; }
dialog.welcome h2 { margin: 0; }
dialog.welcome p { margin: 0; color: var(--muted); line-height: 1.5; }
dialog.welcome small { color: var(--muted); font-size: 12px; line-height: 1.5; }

/* ---------- stickers ---------- */
.toolbar.sticker-mode .swatches, .toolbar.sticker-mode #btnSize { opacity: .35; }
.sticker-sel {
  position: absolute; pointer-events: none; transform-origin: 50% 50%;
  border: 2px dashed var(--accent); border-radius: 8px;
}
.sticker-sel button, .sticker-sel .ss-handle {
  position: absolute; width: 30px; height: 30px; border-radius: 50%; border: 0;
  display: grid; place-items: center; pointer-events: auto; touch-action: none;
  background: var(--accent); color: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.25);
}
.sticker-sel svg { width: 16px; height: 16px; stroke-width: 2.4; }
.ss-del { left: -15px; top: -15px; }
.ss-handle { right: -15px; bottom: -15px; cursor: nwse-resize; }

dialog.sticker-sheet { width: min(560px, calc(100vw - 24px)); }
.pack-tabs { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: thin; }
.pack-tab {
  flex: none; display: flex; align-items: center; gap: 6px; padding: 5px 12px 5px 6px;
  border: 0; border-radius: 999px; background: #f1ebe0; font-size: 14px; white-space: nowrap;
}
.pack-tab img { width: 26px; height: 26px; }
.pack-tab.active { background: var(--accent); color: #fff; }
.pack-tab.locked::after { content: "🔒"; font-size: 11px; }
.pack-lock {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 8px 0 0;
  padding: 10px 12px; border-radius: 12px; background: #fff4e0; font-size: 14px;
}
.pack-lock[hidden] { display: none; }
.pack-lock span { color: var(--muted); flex: 1; }
.pack-lock .btn { font-size: 13px; padding: 6px 12px; opacity: .6; }
.sticker-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 6px;
  max-height: min(50vh, 420px); overflow-y: auto; margin-top: 10px; padding: 2px;
}
.sticker-item {
  aspect-ratio: 1; border: 0; border-radius: 14px; background: transparent; padding: 6px;
  transition: transform .12s, background .12s;
}
.sticker-item:hover { background: rgba(0,0,0,.05); transform: scale(1.08); }
.sticker-item img { width: 100%; height: 100%; object-fit: contain; }
.sticker-item.locked { filter: grayscale(.6) opacity(.5); cursor: default; transform: none; }
.empty { grid-column: 1 / -1; text-align: center; color: var(--muted); }
.about a { color: inherit; }

/* ---------- Google Calendar ---------- */
.dh-events { display: flex; flex-direction: column; align-items: center; gap: 3px; margin-top: 6px; }
.dh-events[hidden] { display: none; }
.ev-chip {
  max-width: 100%; display: flex; align-items: center; gap: 6px;
  border: 0; border-radius: 999px; padding: 3px 12px 3px 8px;
  background: rgba(255, 255, 255, .75); box-shadow: inset 0 0 0 1px var(--line);
  font-size: 14px; color: var(--ink); white-space: nowrap;
}
.ev-chip span { overflow: hidden; text-overflow: ellipsis; }
.ev-chip b { font-weight: 600; font-variant-numeric: tabular-nums; }
.ev-dot { flex: none; width: 9px; height: 9px; border-radius: 50%; }
.ev-more { font-size: 13px; color: var(--muted); background: transparent; border: 0; padding: 2px 8px; }
/* portrait: keep the header short */
.dh-events .ev-chip:nth-of-type(n+3) { display: none; }
@media (min-width: 760px) and (min-aspect-ratio: 5/4) {
  .dh-events .ev-chip:nth-of-type(n+3) { display: flex; }
  .dh-events .ev-chip:nth-of-type(n+5) { display: none; }
}

.ev-list { display: flex; flex-direction: column; max-height: min(55vh, 460px); overflow-y: auto; }
.ev-item { display: flex; align-items: flex-start; gap: 10px; padding: 10px 2px; border-bottom: 1px solid var(--line); }
.ev-item .ev-dot { margin-top: 7px; }
.ev-body { flex: 1; min-width: 0; }
.ev-body b { display: block; font-weight: 600; }
.ev-body small { display: block; color: var(--muted); font-size: 13px; }
.ev-item button {
  flex: none; border: 0; border-radius: 999px; padding: 5px 12px; font-size: 13px;
  background: #f1ebe0; color: var(--ink);
}
.ev-item button:disabled { opacity: .5; }

.cal-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--line); }
.cal-row b { display: block; font-weight: 600; font-size: 15px; }
.cal-row small { display: block; color: var(--muted); font-size: 12px; }
.cal-row .btn { flex: none; text-decoration: none; font-size: 14px; padding: 7px 14px; }

.month-grid button.ev::before {
  content: ""; position: absolute; top: 5px; right: 6px; width: 6px; height: 6px; border-radius: 50%; background: #1a73e8;
}
.legend .dot.ev { background: #1a73e8; }
