:root {
  color-scheme: light;
  --page: #f7f8fb;
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --surface-soft: #f1f4f8;
  --ink: #172033;
  --muted: #687386;
  --line: #d9dee8;
  --line-strong: #bfc7d5;
  --accent: #70a5ff;
  --accent-hover: #5e96f4;
  --accent-soft: #eaf2ff;
  --focus: rgba(112, 165, 255, 0.24);
  --rail: #3f70bb;
  --rail-hover: #345f9f;
  --danger: #c63c53;
  --success: #177451;
  --shadow: 0 18px 50px rgba(20, 30, 52, 0.18);
  --canvas-filter: none;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #10131a;
  --surface: #171b24;
  --surface-raised: #1d222d;
  --surface-soft: #242a36;
  --ink: #e8ebf2;
  --muted: #a0a8b8;
  --line: #303746;
  --line-strong: #465064;
  --accent: #70a5ff;
  --accent-hover: #8bb6ff;
  --accent-soft: #243653;
  --focus: rgba(112, 165, 255, 0.3);
  --rail: #365f99;
  --rail-hover: #4273b5;
  --danger: #ff8394;
  --success: #70d5ab;
  --shadow: 0 22px 60px rgba(0, 0, 0, 0.42);
  --canvas-filter: invert(0.89) hue-rotate(178deg) saturate(0.72) brightness(0.82) contrast(1.08);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body {
  background: var(--page);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 10px;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

#header {
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  display: flex;
  width: 100%;
  height: 30px;
  padding: 3px 7px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  box-shadow: 0 1px 7px rgba(22, 32, 51, 0.06);
  backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  min-width: 0;
  height: 24px;
  align-items: center;
  color: var(--ink);
  text-decoration: none;
}
#logo { display: block; width: 83px; height: 20px; object-fit: contain; object-position: left center; }

#navbar { display: flex; min-width: 0; margin-left: auto; align-items: center; justify-content: flex-end; gap: 4px; }
.btn {
  display: inline-flex;
  height: 23px;
  margin: 0;
  padding: 0 9px;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  transition: border-color 120ms ease, background-color 120ms ease, color 120ms ease, transform 120ms ease;
}
.btn:hover { border-color: var(--accent-hover); background: var(--accent-hover); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .auth-close:focus-visible, .auth-tab:focus-visible, .auth-submit:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus); }
.btn-primary { border-color: var(--accent); background: var(--accent); color: #fff; }
.btn-primary:hover { border-color: var(--accent-hover); background: var(--accent-hover); }
.icon-btn { width: 27px; padding: 0; }
#newsheet span { color: #fff; font-size: 16px; font-weight: 500; transform: translateY(-1px); }
#theme-toggle svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.8; }
.theme-moon { display: none; }
:root[data-theme="dark"] .theme-sun { display: none; }
:root[data-theme="dark"] .theme-moon { display: block; }
#auth-user { max-width: 170px; overflow: hidden; padding: 0 4px; color: var(--muted); font-size: 10px; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }

#workspace { position: fixed; inset: 30px 0 30px; }
#maingrid,
#sidegrid {
  position: fixed;
  z-index: 1;
  top: 30px;
  bottom: 30px;
  left: 0;
  display: block;
  outline: none;
  background: #fff;
  filter: var(--canvas-filter);
  transform: translateZ(0);
}
#maingrid:focus-visible,
#sidegrid:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

#side-grid-panel {
  position: fixed;
  z-index: 18;
  top: 30px;
  right: 10px;
  bottom: 30px;
  width: 20vw;
  overflow: hidden;
  border-left: 1px solid var(--line-strong);
  background: var(--surface);
}
#sidegrid {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
#side-query {
  position: absolute;
  z-index: 2;
  top: 3px;
  right: 20px;
  left: 4px;
  height: 24px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 5px;
  outline: none;
  background: var(--surface-raised);
  color: var(--ink);
  font-size: 11px;
  line-height: 22px;
}
#side-query::placeholder { color: var(--muted); opacity: 0.72; }
#side-query:hover { border-color: var(--line-strong); }
#side-query:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--focus);
}
#side-grid-toggle {
  position: fixed;
  z-index: 95;
  top: 30px;
  right: 0;
  bottom: 0;
  width: 10px;
  padding: 0;
  border: 0;
  border-left: 1px solid color-mix(in srgb, var(--rail) 75%, var(--line));
  background: var(--rail);
  color: #fff;
  cursor: pointer;
  transition: background-color 120ms ease;
}
#side-grid-toggle:hover { background: var(--rail-hover); }
#side-grid-toggle:focus-visible { outline: 2px solid #fff; outline-offset: -3px; }
#side-grid-toggle span {
  position: absolute;
  top: 50%;
  left: 50%;
  font-size: 15px;
  line-height: 1;
  transform: translate(-50%, -50%);
}
body.side-grid-open #side-grid-toggle span { transform: translate(-50%, -50%) rotate(180deg); }
body.side-grid-open #selection { right: calc(20vw + 20px); }
body.side-grid-open #sheet-tabs { right: calc(20vw + 10px); }

#selector, #query {
  position: fixed;
  z-index: 25;
  top: 33px;
  height: 24px;
  border: 1px solid transparent;
  border-radius: 5px;
  outline: none;
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  color: var(--ink);
  line-height: 22px;
  transition: border-color 120ms ease, background-color 120ms ease, box-shadow 120ms ease;
}
#selector:hover, #query:hover { border-color: var(--line); background: var(--surface); }
#selector:focus, #query:focus { border-color: var(--accent); background: var(--surface); box-shadow: 0 0 0 3px var(--focus); }
#selector { left: 5px; width: 61px; padding: 0 6px; color: var(--muted); font-size: 11px; font-weight: 700; text-align: center; }
#query { left: 139px; width: min(650px, calc(100vw - 360px)); padding: 0 9px; font-size: 11px; }
#query::placeholder { color: var(--muted); opacity: 0.72; }

#selection {
  position: fixed;
  z-index: 24;
  top: 35px;
  right: 20px;
  min-width: 55px;
  max-width: 170px;
  min-height: 20px;
  padding: 3px 8px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  line-height: 12px;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(20, 30, 52, 0.08);
  pointer-events: none;
}
#selection:empty { display: none; }

#sheet-tabs {
  position: fixed;
  z-index: 90;
  right: 10px;
  bottom: 0;
  left: 0;
  display: flex;
  height: 30px;
  padding: 0 8px;
  align-items: flex-start;
  gap: 3px;
  overflow-x: auto;
  overflow-y: hidden;
  border-top: 1px solid var(--line);
  background: var(--surface-soft);
}
.sheet-tab,
#add-sheet-tab {
  flex: 0 0 auto;
  height: 25px;
  margin: 0;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
.sheet-tab {
  min-width: 92px;
  padding: 0 20px;
  border: 1px solid var(--line);
  border-top: 0;
  border-radius: 0 0 6px 6px;
  background: transparent;
  color: var(--muted);
}
.sheet-tab:hover { color: var(--accent); }
.sheet-tab.is-active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: inset 0 -2px 0 var(--accent);
}
.sheet-tab:focus-visible,
#add-sheet-tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
#add-sheet-tab {
  width: 36px;
  padding: 0;
  border: 1px solid var(--accent);
  border-top: 0;
  border-radius: 0 0 6px 6px;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 500;
  line-height: 1;
}
#add-sheet-tab:hover { border-color: var(--accent-hover); background: var(--accent-hover); }

#ieditor {
  position: fixed;
  z-index: 50;
  display: none;
  min-width: 65px;
  min-height: 20px;
  padding: 2px 5px;
  overflow: hidden;
  border: 1px solid var(--accent) !important;
  border-radius: 3px;
  outline: none;
  background: var(--surface) !important;
  color: var(--ink);
  font: 12px/15px ui-sans-serif, sans-serif;
  white-space: nowrap;
  box-shadow: 0 0 0 2px var(--focus), 0 4px 12px rgba(20, 30, 52, 0.16);
}
#promptr {
  position: fixed;
  z-index: 60;
  top: 58px;
  left: 5px;
  display: none;
  width: 210px;
  max-height: 160px;
  padding: 4px;
  overflow: auto;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  outline: none;
  background: var(--surface-raised);
  color: var(--ink);
  font-size: 11px;
  box-shadow: var(--shadow);
}
#promptr option { padding: 5px 7px; border-radius: 4px; }

.auth-modal { position: fixed; z-index: 1000; inset: 0; display: grid; padding: 20px; place-items: center; }
.auth-backdrop { position: absolute; inset: 0; width: 100%; height: 100%; padding: 0; border: 0; background: rgba(8, 12, 21, 0.62); backdrop-filter: blur(6px); cursor: default; }
.auth-dialog { position: relative; width: min(100%, 400px); padding: 30px; border: 1px solid var(--line); border-radius: 16px; background: var(--surface-raised); color: var(--ink); box-shadow: var(--shadow); animation: auth-dialog-in 160ms ease-out; }
@keyframes auth-dialog-in { from { opacity: 0; transform: translateY(8px) scale(0.985); } }
.auth-close { position: absolute; top: 12px; right: 12px; width: 32px; height: 32px; padding: 0; border: 0; border-radius: 50%; background: transparent; color: var(--muted); font: 400 25px/30px Arial, sans-serif; cursor: pointer; }
.auth-close:hover { background: var(--surface-soft); color: var(--ink); }
.auth-dialog h1 { margin: 0 0 6px; color: var(--ink); font-size: 24px; line-height: 1.2; }
.auth-dialog > p { margin: 0 0 22px; color: var(--muted); font-size: 13px; line-height: 1.5; }
.auth-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin-bottom: 22px; padding: 4px; border-radius: 9px; background: var(--surface-soft); }
.auth-tab { min-height: 36px; padding: 7px 10px; border: 0; border-radius: 7px; background: transparent; color: var(--muted); font-size: 12px; font-weight: 700; cursor: pointer; }
.auth-tab.is-active { background: var(--surface-raised); color: var(--accent); box-shadow: 0 2px 7px rgba(20, 30, 52, 0.1); }
#auth-form { display: grid; }
#auth-form label { margin: 0 0 6px; color: var(--ink); font-size: 12px; font-weight: 700; }
#auth-form input { width: 100%; height: 43px; margin: 0 0 16px; padding: 9px 11px; border: 1px solid var(--line-strong); border-radius: 8px; outline: none; background: var(--surface); color: var(--ink); font-size: 14px; transition: border-color 120ms ease, box-shadow 120ms ease; }
#auth-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus); }
#auth-form input[aria-invalid="true"] { border-color: var(--danger); box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 18%, transparent); }
.auth-status { min-height: 18px; margin: -3px 0 10px; color: var(--danger); font-size: 12px; line-height: 1.45; }
.auth-status.is-success { color: var(--success); }
.auth-submit { min-height: 43px; padding: 10px 14px; border: 1px solid var(--accent); border-radius: 8px; background: var(--accent); color: #fff; font-size: 13px; font-weight: 700; cursor: pointer; transition: background-color 120ms ease, border-color 120ms ease, transform 120ms ease; }
.auth-submit:hover:not(:disabled) { border-color: var(--accent-hover); background: var(--accent-hover); }
.auth-submit:active:not(:disabled) { transform: translateY(1px); }
.auth-submit:disabled { cursor: wait; opacity: 0.68; }

@media (max-width: 760px) {
  #auth-user { display: none; }
  #selection { right: 18px; max-width: 100px; }
  #query { width: calc(100vw - 250px); }
}
@media (max-width: 480px) {
  #logo { width: 68px; }
  #signup { display: none; }
  #query { width: calc(100vw - 190px); }
  #selection { display: none; }
  .auth-modal { padding: 12px; }
  .auth-dialog { padding: 24px 20px; border-radius: 13px; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; } }
