:root {
  color-scheme: light;
  --ink: #20242a;
  --muted: #6f756f;
  --line: #d9d2c3;
  --panel: #fffaf1;
  --panel-strong: #f2eadb;
  --paper: #fffdf8;
  --accent: #176b5c;
  --accent-strong: #0d4e44;
  --danger: #a03d36;
  --shadow: 0 18px 50px rgba(39, 34, 25, .13);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background: #e8e2d5;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  height: 100dvh;
  overflow: hidden;
  background:
    linear-gradient(90deg, rgba(255,255,255,.55), rgba(255,255,255,0)),
    #e7dfd1;
}

.app-shell.sidebar-collapsed {
  grid-template-columns: 0 minmax(0, 1fr);
}

.sidebar-backdrop {
  display: none;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  width: 300px;
  gap: 18px;
  padding: 18px;
  border-right: 1px solid var(--line);
  background: rgba(255, 250, 241, .88);
  backdrop-filter: blur(14px);
  overflow: hidden;
  transition: transform .22s ease, opacity .22s ease;
  z-index: 10;
}

.app-shell.sidebar-collapsed .sidebar {
  transform: translateX(-100%);
  opacity: 0;
  pointer-events: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand > div:nth-child(2) {
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 42px;
  height: 42px;
  border-radius: 8px;
  color: #fff;
  background: #22352f;
  font-weight: 800;
}

.brand strong,
.brand span {
  display: block;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand span {
  color: var(--muted);
  font-size: 12px;
}

.sidebar-close {
  margin-left: auto;
  flex: 0 0 38px;
  width: 38px;
  min-height: 38px;
}

.sync-chip {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  width: fit-content;
  max-width: 100%;
  padding: 0 10px;
  border: 1px solid rgba(23, 107, 92, .22);
  border-radius: 8px;
  color: var(--accent-strong);
  background: rgba(23, 107, 92, .08);
  font-size: 12px;
  font-weight: 800;
}

.sync-chip[data-state="offline"],
.sync-chip[data-state="pending"],
.sync-chip[data-state="dirty"] {
  color: #8a4a12;
  border-color: rgba(185, 135, 19, .32);
  background: rgba(242, 201, 76, .18);
}

.sync-chip[data-state="syncing"] {
  color: #175b8d;
  border-color: rgba(23, 91, 141, .28);
  background: rgba(83, 166, 220, .14);
}

.sidebar-actions,
.export-menu,
.toolbar,
.page-strip,
.page-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.primary-btn,
.secondary-btn,
.icon-btn,
.tool,
.page-add {
  min-height: 40px;
  border-radius: 8px;
  transition: transform .15s ease, background .15s ease, color .15s ease, box-shadow .15s ease;
}

.primary-btn {
  padding: 0 14px;
  color: #fff;
  background: var(--accent);
  font-weight: 700;
}

.secondary-btn {
  padding: 0 12px;
  color: var(--ink);
  background: #f1eadf;
  border: 1px solid #d5ccbd;
  font-weight: 650;
  white-space: nowrap;
}

.icon-btn,
.tool,
.page-add {
  display: grid;
  place-items: center;
  flex: 0 0 42px;
  width: 42px;
  color: var(--ink);
  background: #f4ecdf;
  border: 1px solid #d8cfbf;
  font-size: 18px;
}

.primary-btn:hover,
.secondary-btn:hover,
.icon-btn:hover,
.tool:hover,
.page-add:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 18px rgba(35, 31, 24, .11);
}

.tool.active {
  color: #fff;
  background: #22352f;
  border-color: #22352f;
}

.tool svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.danger {
  color: #fff;
  background: var(--danger);
  border-color: var(--danger);
}

.danger-soft {
  color: var(--danger);
  border-color: rgba(160, 61, 54, .35);
}

.search-box {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.search-box input,
.note-title,
.select-control,
.mini-select {
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: rgba(255,255,255,.78);
  outline: none;
}

.search-box input {
  height: 42px;
  padding: 0 12px;
}

.note-list {
  display: grid;
  align-content: start;
  gap: 10px;
  min-height: 0;
  overflow: auto;
  padding-right: 2px;
}

.note-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px;
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--ink);
  background: rgba(255,255,255,.52);
}

.note-item.active {
  border-color: #b7aa95;
  background: #fffaf1;
  box-shadow: 0 8px 26px rgba(63, 55, 41, .11);
}

.note-select {
  display: grid;
  gap: 5px;
  min-width: 0;
  padding: 4px;
  color: inherit;
  background: transparent;
  text-align: left;
}

.note-delete {
  width: 34px;
  min-height: 34px;
  font-size: 16px;
  opacity: .72;
}

.note-delete:hover {
  opacity: 1;
}

.note-select strong,
.note-select span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.note-select span {
  color: var(--muted);
  font-size: 12px;
}

.workspace {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  height: 100dvh;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(210px, 360px) minmax(0, 1fr);
  align-items: start;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 250, 241, .72);
  backdrop-filter: blur(14px);
  min-width: 0;
  z-index: 5;
}

.topbar-main {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: start;
  gap: 10px;
  min-width: 0;
}

.menu-toggle {
  margin-top: 1px;
}

.title-area {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.note-title {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  font-size: 18px;
  font-weight: 800;
  background: transparent;
}

.mini-select {
  width: min(180px, 100%);
  height: 28px;
  padding: 0 8px;
  color: var(--muted);
  background: rgba(255,255,255,.62);
  font-size: 12px;
  font-weight: 700;
}

.save-state {
  color: var(--muted);
  font-size: 12px;
}

.toolbar-row {
  display: flex;
  min-width: 0;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 4px;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}

.toolbar,
.export-menu {
  flex: 0 0 auto;
}

.range-control {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 7px;
  height: 40px;
  padding: 0 10px;
  border: 1px solid #d8cfbf;
  border-radius: 8px;
  background: #f4ecdf;
}

.range-control span {
  min-width: 22px;
  text-align: center;
  font-size: 12px;
  font-weight: 800;
}

.range-control input {
  width: 74px;
  accent-color: var(--accent);
}

.swatch {
  display: grid;
  place-items: center;
  width: 42px;
  flex: 0 0 42px;
  height: 40px;
  border: 1px solid #d8cfbf;
  border-radius: 8px;
  background: #f4ecdf;
}

.swatch input {
  width: 26px;
  height: 26px;
  padding: 0;
  border: 0;
  background: transparent;
}

.select-control {
  flex: 0 0 152px;
  height: 40px;
  padding: 0 10px;
  background: #f4ecdf;
}

.compact-select {
  flex-basis: 128px;
}

.toggle-control {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 40px;
  padding: 0 10px;
  border: 1px solid #d8cfbf;
  border-radius: 8px;
  background: #f4ecdf;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.toggle-control input {
  accent-color: var(--accent);
}

.toggle-control.active-toggle {
  color: #fff;
  background: #22352f;
  border-color: #22352f;
}

.toggle-control.active-toggle input {
  accent-color: #55b9a3;
}

.canvas-stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  background: var(--paper);
}

#paperCanvas {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: var(--paper);
  border-radius: 0;
  box-shadow: none;
  transform-origin: center center;
  touch-action: none;
}

.zoom-chip {
  position: absolute;
  z-index: 3;
  right: 18px;
  top: 18px;
  padding: 7px 10px;
  border: 1px solid #d2c7b4;
  border-radius: 8px;
  color: var(--muted);
  background: rgba(255, 250, 241, .88);
  font-size: 12px;
  font-weight: 800;
}

.bottom-panel {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) auto;
  gap: 12px;
  align-items: stretch;
  padding: 10px 14px;
  border-top: 1px solid var(--line);
  background: rgba(255, 250, 241, .76);
  z-index: 4;
}

.page-strip {
  min-width: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.page-actions {
  justify-content: flex-end;
  flex-wrap: nowrap;
}

.page-pill {
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-width: 128px;
  height: 52px;
  padding: 5px 9px 5px 6px;
  border: 1px solid #d5ccbd;
  border-radius: 8px;
  color: var(--ink);
  background: #f4ecdf;
  font-weight: 750;
  text-align: left;
}

.page-pill.active {
  color: #fff;
  background: #22352f;
  border-color: #22352f;
}

.page-thumb {
  width: 28px;
  height: 38px;
  border: 1px solid rgba(40, 35, 27, .22);
  border-radius: 3px;
  background: #fffdf8;
  object-fit: cover;
}

.page-pill span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

dialog {
  width: min(420px, calc(100vw - 34px));
  border: 0;
  border-radius: 8px;
  color: var(--ink);
  background: #fffaf1;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(30, 26, 20, .34);
}

dialog h2 {
  margin: 0 0 8px;
}

dialog p {
  color: var(--muted);
  line-height: 1.45;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --ink: #eee9de;
    --muted: #aaa293;
    --line: #494237;
    --panel: #27231d;
    --panel-strong: #312b23;
    --paper: #1f211f;
    --accent: #55b9a3;
    --accent-strong: #78d7c2;
    --shadow: 0 18px 50px rgba(0, 0, 0, .34);
  }

  html,
  body,
  .canvas-stage {
    background-color: #1c1b19;
  }

  .sidebar,
  .topbar,
  .bottom-panel {
    background: rgba(39, 35, 29, .86);
  }

  .search-box input,
  .note-title,
  .mini-select,
  .select-control,
  .note-item,
  dialog {
    background: rgba(31, 29, 25, .82);
  }
}

@media (max-width: 1360px) {
  .topbar {
    grid-template-columns: minmax(190px, 320px) minmax(0, 1fr);
    align-items: stretch;
  }

  .toolbar,
  .export-menu {
    flex-wrap: nowrap;
    overflow: visible;
  }

  .range-control input {
    width: 70px;
  }
}

@media (max-width: 1180px) {
  .app-shell {
    display: block;
    height: 100dvh;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(320px, calc(100vw - 54px));
    max-width: 86vw;
    padding: max(14px, env(safe-area-inset-top)) 16px max(14px, env(safe-area-inset-bottom));
    box-shadow: 14px 0 46px rgba(35, 31, 24, .18);
  }

  .workspace {
    width: 100%;
    height: 100dvh;
    grid-template-rows: auto minmax(0, 1fr) auto;
  }

  .app-shell.sidebar-collapsed .sidebar {
    display: flex;
  }

  .app-shell:not(.sidebar-collapsed) .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 9;
    background: rgba(30, 26, 20, .26);
  }

  .topbar {
    grid-template-columns: 1fr;
    align-items: stretch;
    gap: 8px;
    padding: 8px max(10px, env(safe-area-inset-right)) 8px max(10px, env(safe-area-inset-left));
  }

  .topbar-main {
    grid-template-columns: 44px minmax(0, 1fr);
    align-items: center;
  }

  .menu-toggle {
    margin-top: 0;
  }

  .title-area {
    grid-template-columns: minmax(170px, 1fr) 132px minmax(92px, auto);
    align-items: center;
    gap: 8px;
  }

  .note-title {
    height: 38px;
    font-size: 16px;
  }

  .mini-select {
    width: 132px;
    height: 38px;
  }

  .save-state {
    min-width: 92px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: right;
  }

  .toolbar,
  .export-menu {
    flex-wrap: nowrap;
    overflow: visible;
    padding-bottom: 2px;
  }

  .toolbar-row {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 5px;
    scroll-padding-inline: 10px;
  }

  .canvas-stage {
    min-height: 0;
  }

  .bottom-panel {
    grid-template-columns: 1fr auto;
    gap: 8px;
    padding: 8px max(10px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  }

  .page-actions {
    overflow-x: auto;
    max-width: 230px;
    padding-bottom: 2px;
  }
}

@media (min-width: 721px) and (max-width: 1180px) {
  .primary-btn,
  .secondary-btn,
  .icon-btn,
  .tool,
  .page-add,
  .range-control,
  .select-control,
  .toggle-control,
  .swatch {
    min-height: 44px;
  }

  .icon-btn,
  .tool,
  .page-add,
  .swatch {
    flex-basis: 44px;
    width: 44px;
  }

  .toolbar-row.compact-writing {
    max-height: 50px;
  }

  .toolbar-row.compact-writing .export-menu,
  .toolbar-row.compact-writing .select-control,
  .toolbar-row.compact-writing .range-control,
  .toolbar-row.compact-writing .toggle-control {
    display: none;
  }

  .page-pill {
    min-width: 118px;
    height: 50px;
  }
}

@media (min-width: 900px) and (max-width: 1180px) and (max-height: 820px) {
  .topbar {
    grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
    align-items: center;
    min-height: 62px;
  }

  .title-area {
    grid-template-columns: minmax(0, 1fr);
  }

  .mini-select,
  .save-state {
    display: none;
  }

  .toolbar-row {
    padding-bottom: 0;
  }

  .bottom-panel {
    min-height: 56px;
    padding-top: 6px;
    padding-bottom: 6px;
  }

  .page-pill {
    height: 46px;
  }
}

@media (max-width: 720px) {
  .workspace {
    grid-template-rows: auto minmax(0, 1fr) auto;
  }

  .topbar {
    gap: 8px;
    padding: 8px;
  }

  .topbar-main {
    grid-template-columns: 40px minmax(0, 1fr);
  }

  .title-area {
    grid-template-columns: minmax(0, 1fr) 112px;
    grid-template-areas:
      "title folder"
      "state state";
    gap: 5px 7px;
  }

  .note-title {
    grid-area: title;
  }

  .mini-select {
    grid-area: folder;
    width: 112px;
  }

  .save-state {
    grid-area: state;
    min-width: 0;
    text-align: left;
  }

  .note-title {
    height: 38px;
    font-size: 16px;
  }

  .toolbar-row {
    gap: 6px;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
  }

  .toolbar-row.compact-writing {
    max-height: 46px;
  }

  .toolbar-row.compact-writing .select-control,
  .toolbar-row.compact-writing .range-control,
  .toolbar-row.compact-writing .toggle-control,
  .toolbar-row.compact-writing .export-menu {
    display: none;
  }

  .primary-btn,
  .secondary-btn,
  .icon-btn,
  .tool,
  .page-add {
    min-height: 38px;
  }

  .icon-btn,
  .tool,
  .page-add {
    flex-basis: 38px;
    width: 38px;
    font-size: 16px;
  }

  .secondary-btn {
    padding: 0 10px;
  }

  .range-control {
    height: 38px;
    gap: 5px;
    padding: 0 7px;
  }

  .range-control input {
    width: 38px;
  }

  .select-control {
    flex-basis: 130px;
    height: 38px;
  }

  .compact-select {
    flex-basis: 116px;
  }

  .toggle-control {
    height: 38px;
    padding: 0 8px;
  }

  .bottom-panel {
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
    padding: 8px;
  }

  .page-actions {
    justify-content: flex-start;
    max-width: none;
    overflow-x: auto;
  }

  .page-pill {
    min-width: 112px;
    height: 46px;
  }

  .page-thumb {
    width: 24px;
    height: 32px;
  }
}
