* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--surface); color: var(--ink);
  font: 14px/1.5 var(--font-ui); display: flex; flex-direction: column;
}
button, select, input, textarea {
  font: inherit; color: var(--ink); background: var(--surface-2);
  border: 1px solid var(--line); border-radius: var(--radius);
}
button { padding: 3px 10px; cursor: pointer; }
button:hover { background: var(--surface-3); }
:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 1px; }
.spacer { flex: 1; }

#toolbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  border-bottom: 1px solid var(--line); background: var(--surface-2);
}
#brand { font-weight: 700; letter-spacing: 0.04em; color: var(--accent); }
#project-title { background: transparent; border-color: transparent; font-weight: 600; min-width: 16ch; }
#project-title:hover, #project-title:focus { border-color: var(--line); background: var(--surface); }

#layout { flex: 1; display: grid; grid-template-columns: 230px 1fr 320px; min-height: 0; }
.col-head { display: flex; align-items: center; gap: 6px; padding: 8px 10px; font-weight: 600; border-bottom: 1px solid var(--line); }

/* Binder */
#binder { border-right: 1px solid var(--line); overflow-y: auto; background: var(--surface); }
#tree, #tree ul { list-style: none; margin: 0; padding: 0 0 0 12px; }
#tree { padding: 6px; }
.node-row {
  display: flex; align-items: center; gap: 6px; padding: 3px 6px;
  border-radius: var(--radius); cursor: pointer; user-select: none;
}
.node-row:hover { background: var(--surface-2); }
.node-row.active { background: var(--sel); }
.node-row .icon { width: 1.2em; text-align: center; color: var(--ink-soft); }
.node-row .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-row .actions { visibility: hidden; display: flex; gap: 2px; }
.node-row:hover .actions { visibility: visible; }
.node-row .actions button { padding: 0 5px; border: none; background: transparent; color: var(--ink-soft); }
.node-row .actions button:hover { color: var(--ink); }
.node-row input.rename { flex: 1; min-width: 0; }

/* Editor */
#editor-col { display: flex; flex-direction: column; min-width: 0; }
#format-bar {
  display: flex; align-items: center; gap: 3px; padding: 5px 10px;
  border-bottom: 1px solid var(--line); background: var(--surface-2); flex-wrap: wrap;
}
#format-bar button { padding: 3px 9px; min-width: 30px; }
#format-bar .fb-sep { width: 1px; height: 18px; background: var(--line); margin: 0 5px; }
.fmt-align.on { background: var(--sel); }

#search-bar {
  display: flex; align-items: center; gap: 6px; padding: 6px 10px;
  border-bottom: 1px solid var(--line); background: var(--surface-2); flex-wrap: wrap;
}
#search-bar[hidden], #search-results[hidden] { display: none; }
#search-input { flex: 1 1 140px; min-width: 0; padding: 5px 8px; }
#search-count { font-size: 12.5px; color: var(--ink-soft); white-space: nowrap; min-width: 3ch; text-align: center; }
#search-bar button { padding: 5px 10px; }
#search-results {
  max-height: 30vh; overflow-y: auto; border-bottom: 1px solid var(--line);
  background: var(--surface); padding: 4px;
}
#search-results .hit { padding: 6px 10px; border-radius: var(--radius); cursor: pointer; font-size: 13px; }
#search-results .hit:hover { background: var(--sel); }
#search-results .hit b { color: var(--accent); }
#page-wrap { flex: 1; overflow-y: auto; display: flex; flex-direction: column; align-items: center; padding: 32px 24px; }
#page-title {
  width: 100%; max-width: var(--page-measure);
  background: transparent; border: none; border-radius: var(--radius);
  color: var(--ink); font-family: var(--font-page);
  font-size: calc(var(--page-size) * 1.55); font-weight: 700; line-height: 1.25;
  text-align: center; padding: 6px 12px; margin-bottom: 10px;
}
#page-title::placeholder { color: var(--ink-soft); font-weight: 400; }
#page-title:hover { background: var(--surface-2); }
#page-title:focus { outline: none; background: var(--surface-2); }
#page-title:disabled { opacity: 0.5; }
#page {
  width: 100%; max-width: var(--page-measure); min-height: 60vh;
  background: var(--page); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 48px clamp(24px, 5vw, 64px); resize: none;
  font-family: var(--font-page); font-size: var(--page-size); line-height: var(--page-leading);
  font-variant-ligatures: var(--page-ligatures); text-align: var(--page-align, left);
}
#page:focus-visible { outline: 1px solid var(--line); }
#status {
  display: flex; gap: 16px; padding: 5px 14px; border-top: 1px solid var(--line);
  color: var(--ink-soft); font-size: 12.5px; background: var(--surface-2);
}
#st-ceiling[data-over="1"] { color: var(--danger); }
#st-saved.dirty { color: var(--warn); }
#credit {
  text-align: center; font-size: 11.5px; color: var(--ink-soft);
  padding: 4px 8px; border-top: 1px solid var(--line); background: var(--surface-2);
  letter-spacing: 0.02em;
}
.tb-break { display: none; }

/* Export checkboxes */
.export-grid label { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--radius); }

/* Distraction-free / focus mode: hide chrome, center the page */
body.focus #toolbar,
body.focus #binder,
body.focus #inspector,
body.focus #format-bar,
body.focus #status,
body.focus #credit,
body.focus #drawer-scrim { display: none !important; }
body.focus #layout { display: block; height: 100%; }
body.focus #editor-col { height: 100%; }
body.focus #page-wrap { padding: 6vh 16px; }
body.focus #page { box-shadow: none; border-color: transparent; }
#focus-exit { display: none; }
body.focus #focus-exit {
  display: block; position: fixed; top: 12px; right: 14px; z-index: 50;
  opacity: 0.35; background: var(--surface-2);
}
body.focus #focus-exit:hover { opacity: 1; }

/* Inspector */
#inspector { border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
#insp-tabs { display: flex; border-bottom: 1px solid var(--line); }
#insp-tabs button { flex: 1; border: none; border-radius: 0; background: var(--surface-2); padding: 7px 4px; color: var(--ink-soft); }
#insp-tabs button.active { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: inset 0 -2px var(--accent); }
#insp-body { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
#insp-body label { display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; color: var(--ink-soft); }
#insp-body input, #insp-body textarea, #insp-body select { padding: 5px 8px; background: var(--surface); }
#insp-body textarea { min-height: 70px; resize: vertical; }
.insp-note { font-size: 12.5px; color: var(--ink-soft); }
.insp-row { display: flex; gap: 8px; }
.insp-row > * { flex: 1; }

/* Findings, snapshots, cuts */
.card {
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 10px; background: var(--surface); display: flex; flex-direction: column; gap: 5px;
}
.card .rule-id { font-weight: 700; font-size: 12px; }
.card[data-sev="error"] .rule-id { color: var(--danger); }
.card[data-sev="warning"] .rule-id { color: var(--warn); }
.card[data-sev="info"] .rule-id { color: var(--ink-soft); }
.card .excerpt { font-family: var(--font-page); }
.card .excerpt mark { background: var(--sel); color: inherit; border-radius: 3px; padding: 0 2px; }
.card .stmt { font-size: 12.5px; color: var(--ink-soft); }
.card .card-actions { display: flex; gap: 6px; }
.card .card-actions .apply { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.kamus-sense { margin: 2px 0; }
.badge { display: inline-block; font-size: 11px; border: 1px solid var(--line); border-radius: 10px; padding: 0 8px; color: var(--ink-soft); }

/* Dialogs */
dialog {
  background: var(--surface); color: var(--ink); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 18px; min-width: 340px;
}
dialog::backdrop { background: rgba(0,0,0,0.35); }
dialog h3 { margin: 0 0 10px; }
dialog label { display: flex; align-items: center; gap: 8px; margin: 8px 0; }
.dialog-actions { display: flex; justify-content: flex-end; margin-top: 12px; }

dialog label.row-check { flex-direction: row; align-items: center; }
#theme-dialog label { justify-content: space-between; }
#theme-dialog input[type="color"] { width: 52px; height: 28px; padding: 0; }

#palette-dialog { width: 480px; padding: 10px; }
#palette-input { width: 100%; padding: 8px 10px; background: var(--surface-2); }
#palette-list { list-style: none; margin: 8px 0 0; padding: 0; max-height: 300px; overflow-y: auto; }
#palette-list li { padding: 6px 10px; border-radius: var(--radius); cursor: pointer; display: flex; gap: 8px; }
#palette-list li .hint { color: var(--ink-soft); font-size: 12px; margin-left: auto; }
#palette-list li.sel, #palette-list li:hover { background: var(--sel); }

/* Export dialog */
.export-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.export-grid button { padding: 10px; }
#custom-fonts { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
#custom-fonts .font-row { display: flex; align-items: center; gap: 8px; }

/* AI chat box */
.chat-box { display: flex; flex-direction: column; gap: 8px; }
.chat-msgs { display: flex; flex-direction: column; gap: 6px; min-height: 60px; max-height: 320px; overflow-y: auto; border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; background: var(--surface); }
.chat-msg { padding: 6px 10px; border-radius: 10px; max-width: 92%; white-space: pre-wrap; font-size: 13px; line-height: 1.45; }
.chat-msg.user { align-self: flex-end; background: var(--sel); }
.chat-msg.ai { align-self: flex-start; background: var(--surface-2); border: 1px solid var(--line); }
.chat-input-row { display: flex; gap: 6px; }
.chat-input-row textarea { flex: 1; min-height: 44px; max-height: 160px; resize: vertical; padding: 6px 8px; }
.chat-msg strong { font-weight: 700; }
.chat-msg em { font-style: italic; }
.chat-msg del { text-decoration: line-through; opacity: 0.8; }
.chat-msg code { font-family: var(--font-ui); background: var(--surface-3); border-radius: 4px; padding: 0 4px; }
.typing { display: inline-flex; gap: 4px; align-items: center; padding: 2px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-soft); display: inline-block; }
@media (prefers-reduced-motion: no-preference) {
  .typing span { animation: budi-blink 1.2s infinite ease-in-out; }
  .typing span:nth-child(2) { animation-delay: 0.2s; }
  .typing span:nth-child(3) { animation-delay: 0.4s; }
}
@keyframes budi-blink { 0%, 80%, 100% { opacity: 0.25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }

/* Account & activity */
#account-dialog { max-width: 560px; width: min(92vw, 560px); }
.activity-list { max-height: 260px; overflow-y: auto; border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 8px; display: flex; flex-direction: column; gap: 4px; }
.activity-row { font-size: 12.5px; border-bottom: 1px solid var(--line); padding: 2px 0; }
.activity-row:last-child { border-bottom: none; }
#login-dialog { max-width: 440px; }
#login-dialog ol { padding-left: 20px; }
#gsi-button { margin: 10px 0; min-height: 44px; }

/* About */
#about-dialog { max-width: 520px; }
#about-content h2 { margin-top: 0; }
#about-content textarea { width: 100%; min-height: 220px; }
#about-content .tagline { color: var(--ink-soft); font-style: italic; }

/* Print: only the prepared print area, as a clean page */
#print-area { display: none; }
@media print {
  body > *:not(#print-area) { display: none !important; }
  #print-area { display: block; font-family: var(--font-page); font-size: 12pt; line-height: 1.6; color: #000; }
  #print-area h1 { font-size: 16pt; }
  #print-area p { margin: 0 0 0.8em; }
  #print-area .doc-break { page-break-before: always; }
}

@media (prefers-reduced-motion: no-preference) {
  .node-row, button, #insp-tabs button { transition: background 0.12s ease; }
  #binder, #inspector { transition: transform 0.18s ease; }
}

/* Mobile: binder & inspector become slide-over drawers */
.mobile-only { display: none; }
@media (max-width: 760px) {
  .mobile-only { display: inline-block; }
  .desktop-only { display: none !important; }
  /* Row 1 = identity (menu · brand · project title), row 2 = actions */
  #toolbar { flex-wrap: wrap; gap: 6px 8px; padding: 6px 8px; align-items: center; }
  #brand { font-size: 15px; flex: 0 0 auto; }
  #project-title { flex: 1 1 6ch; min-width: 0; text-overflow: ellipsis; }
  .tb-break { display: block; flex-basis: 100%; height: 0; margin: 0; }
  #toolbar .spacer { display: none; }
  #btn-toggle-inspector { margin-left: auto; }
  #layout { display: block; position: relative; height: 100%; }
  #editor-col { height: 100%; }
  #page-wrap { padding: 12px 8px; }
  #page { padding: 20px 16px; min-height: 70vh; }
  #binder, #inspector {
    position: fixed; top: 0; bottom: 0; z-index: 30; width: min(85vw, 340px);
    background: var(--surface); transform: translateX(-105%);
    border-right: 1px solid var(--line); box-shadow: 0 0 24px rgba(0,0,0,0.25);
    display: flex; flex-direction: column;
  }
  #binder { left: 0; }
  #inspector { right: 0; left: auto; transform: translateX(105%); border-left: 1px solid var(--line); }
  #binder.open, #inspector.open { transform: translateX(0); }
  #binder { overflow-y: auto; }
  #drawer-scrim {
    position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,0.35);
  }
  #insp-tabs { flex-wrap: wrap; }
  /* search bar: full-width input on its own line, controls below */
  #search-input { flex: 1 1 100%; order: -1; }
  #format-bar { justify-content: flex-start; }
  /* touch targets */
  button { padding: 6px 12px; }
  .node-row { padding: 8px 6px; }
  .node-row .actions { visibility: visible; }
  #status { flex-wrap: wrap; gap: 10px; }
}
