/* Editor shell: 44 px top bar, workspace (tabbed left rail, Design|Split|Code
   center, right Inspector), 28 px status bar, tablet drawer, and <=720 px
   dedicated phone layout (bottom bar + sheets). */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: var(--ui-size);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: var(--space-2);
  top: -48px;
  z-index: 100;
  padding: var(--space-2) var(--space-3);
  background: var(--raised);
  color: var(--accent);
  border-radius: var(--radius);
  box-shadow: var(--shadow-pop);
}

.skip-link:focus {
  top: var(--space-2);
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
  border-radius: var(--radius-sm);
}

.app {
  display: grid;
  grid-template-rows: auto var(--bar-h) auto minmax(0, 1fr) var(--status-h);
  grid-template-areas: "tabs" "top" "notices" "work" "status";
  height: 100vh;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
}

/* ── Document tab bar ─────────────────────────────────── */

.tab-bar {
  grid-area: tabs;
  display: flex;
  align-items: center;
  gap: 2px;
  height: 28px;
  padding: 0 6px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  min-width: 0;
}

.tab-bar-btn {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
}

.tab-bar-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.tab-bar-btn .icon {
  width: 14px;
  height: 14px;
}

.tab-list {
  display: flex;
  align-items: center;
  gap: 2px;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}

.tab-list::-webkit-scrollbar {
  display: none;
}

.doc-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 190px;
  height: 26px;
  padding: 0 8px;
  border-radius: 6px;
  color: var(--text-muted);
  font-size: var(--t-12);
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  flex: none;
}

.doc-tab:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.doc-tab.on {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 0 0 1px var(--border);
}

.doc-tab .icon {
  width: 13px;
  height: 13px;
  color: var(--text-faint);
}

.doc-tab-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-tab-dot {
  flex: none;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--warning);
}

.doc-tab-close {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 16px;
  height: 16px;
  padding: 0;
  margin-right: -2px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
}

.doc-tab-close:hover {
  background: var(--surface-active);
  color: var(--text);
}

.doc-tab-close .icon {
  width: 11px;
  height: 11px;
}

@media (max-width: 720px) {
  .tab-bar {
    display: none !important;
  }
}

/* ── Top bar ───────────────────────────────────────────── */

.topbar {
  grid-area: top;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 0 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.bar-left,
.bar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.bar-right {
  justify-content: flex-end;
}

.logo-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 30px;
  padding: 0 6px 0 6px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
}

.logo-btn:hover,
.logo-btn[aria-expanded="true"] {
  background: var(--surface-hover);
  color: var(--text);
}

.logo-btn img {
  width: 22px;
  height: 22px;
  padding: 3px;
  border-radius: 5px;
  background: #ffffff;
  box-shadow: 0 0 0 1px rgb(11 26 46 / 0.08);
}

.doc-title {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.doc-name {
  font-weight: 600;
  font-size: var(--t-13);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-name.editable {
  padding: 2px 5px;
  margin: -2px -5px;
  border-radius: var(--radius-sm);
  cursor: text;
}

.doc-name.editable:hover {
  background: var(--surface-hover);
}

.doc-name-input {
  width: 160px;
  height: 24px;
  padding: 0 6px;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: 600 var(--t-13)/1 var(--font-ui);
  outline: none;
}

.dirty-mark {
  flex: none;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--warning);
}

.doc-state {
  color: var(--text-faint);
  font-size: var(--t-11);
  white-space: nowrap;
}

.doc-state.dirty {
  color: var(--warning);
}

.tools-pill {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.tools-pill .tool {
  width: 30px;
  height: 28px;
  border-radius: 6px;
}

.tools-pill .tool.on {
  background: var(--accent);
  color: var(--accent-text);
}

.pill-sep {
  width: 1px;
  height: 16px;
  background: var(--border-strong);
  margin: 0 2px;
}

.undo-redo {
  display: flex;
  gap: 2px;
}

.undo-redo .tool {
  position: relative;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.undo-redo .tool.pressing::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  height: 2px;
  background: var(--accent);
  animation: hold-bar 450ms linear forwards;
}

@keyframes hold-bar {
  from {
    width: 0;
  }
  to {
    width: 100%;
  }
}

.app[data-view="code"] .tools-pill,
.app[data-view="code"] .zoom-btn {
  visibility: hidden;
}

/* ── Notices ───────────────────────────────────────────── */

.banners {
  grid-area: notices;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* ── Workspace ─────────────────────────────────────────── */

.workspace {
  grid-area: work;
  display: flex;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  overflow: hidden;
}

.panel-left {
  flex: 0 0 var(--panel-left);
  border-right: 1px solid var(--border);
}

.panel-right {
  flex: 0 0 var(--panel-right);
  border-left: 1px solid var(--border);
}

.app[data-left="closed"] .panel-left,
.app[data-right="closed"] .panel-right,
.app[data-view="code"] .panel-right {
  display: none;
}

.reopen-left,
.reopen-right {
  display: none;
  position: absolute;
  top: 10px;
  z-index: 10;
  width: 30px;
  height: 30px;
  place-items: center;
  border: 0;
  border-radius: var(--radius);
  background: var(--raised);
  color: var(--text-muted);
  box-shadow: var(--shadow-pop);
  cursor: pointer;
}

.reopen-left {
  left: 10px;
}

.reopen-right {
  right: 10px;
}

.reopen-left:hover,
.reopen-right:hover {
  color: var(--text);
}

.app[data-left="closed"] .reopen-left {
  display: grid;
}

.app[data-right="closed"]:not([data-view="code"]) .reopen-right {
  display: grid;
}

.panel-right-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 38px;
  padding: 0 4px 0 12px;
  flex: none;
  border-bottom: 1px solid var(--border);
}

.panel-right-title {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-muted);
}

.panel-right-head .collapse {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
}

.panel-right-head .collapse:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.center {
  --split: 0.5;
  display: flex;
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  position: relative;
}

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--surface);
  position: relative;
}

.pane-code {
  flex: 0 0 calc(var(--split) * 100% - 3px);
}

.pane-canvas {
  flex: 1 1 0;
  background: var(--canvas-bg);
}

.code-host {
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
}

.code-host .cm-editor {
  height: 100%;
}

.pane-head {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  height: 36px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.pane-title {
  margin: 0;
  font-size: var(--t-11);
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

.pane-meta {
  flex: 1 1 auto;
  min-width: 0;
  color: var(--text-faint);
  font-size: var(--t-11);
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
}

/* Divider */

.divider {
  position: relative;
  flex: 0 0 6px;
  margin: 0 -2px;
  z-index: 5;
  cursor: col-resize;
  touch-action: none;
  background: transparent;
}

.divider::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 1px;
  transform: translateX(-50%);
  background: var(--border);
  transition: background var(--dur) var(--ease), width var(--dur) var(--ease);
}

.divider:hover::before,
.divider.dragging::before,
.divider:focus-visible::before {
  width: 3px;
  background: var(--accent);
}

.divider:focus-visible {
  outline: none;
}

.divider-grip {
  display: none;
}

/* View states */

.app[data-view="design"] .pane-code,
.app[data-view="design"] .divider,
.app[data-source="closed"] .pane-code,
.app[data-source="closed"] .divider {
  display: none;
}

.app[data-view="code"] .divider,
.app[data-view="code"] .pane-canvas {
  display: none;
}

.app[data-view="code"] .pane-code {
  flex: 1 1 0;
}

/* ── Status bar ────────────────────────────────────────── */

.statusbar {
  grid-area: status;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  padding: 0 8px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  color: var(--text-faint);
  font-size: var(--t-11);
  white-space: nowrap;
}

.status-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
}

.status-item .icon {
  width: 13px;
  height: 13px;
}

.status-spacer {
  flex: 1 1 auto;
}

.muted {
  color: var(--text-faint);
}

/* ── Phone bottom bar & sheets (hidden on desktop) ─────── */

.phone-bar,
.sheet {
  display: none;
}

/* ── Laptop (<= 1240 px) ───────────────────────────────── */

@media (max-width: 1240px) {
  .seg button span {
    display: none;
  }

  .doc-state {
    display: none;
  }
}

/* ── Tablet (721–860 px) ───────────────────────────────── */

@media (max-width: 860px) {
  .panel-left {
    display: none;
  }

  .reopen-left {
    display: grid;
  }

  .app.left-drawer .panel-left {
    display: flex;
    position: fixed;
    left: 0;
    top: var(--bar-h);
    bottom: var(--status-h);
    z-index: 35;
    width: 240px;
    box-shadow: var(--shadow-pop);
  }

  .tools-pill .pill-sep,
  .tools-pill .tool[data-tool="hand"] {
    display: none;
  }
}

/* ── Phone (<= 720 px) ─────────────────────────────────── */

@media (max-width: 720px) {
  .app {
    grid-template-rows: var(--bar-h) auto minmax(0, 1fr) auto;
    grid-template-areas: "top" "notices" "work" "phone";
  }

  .topbar {
    grid-template-columns: 1fr auto;
    gap: 6px;
    padding: 0 6px;
    padding-top: env(safe-area-inset-top);
  }

  .tools-pill,
  .seg,
  .menu-wrap:has(#zoom-btn),
  .panel-left,
  .panel-right,
  .statusbar,
  .reopen-left {
    display: none !important;
  }

  .doc-name {
    font-size: 14px;
    max-width: 36vw;
  }

  .undo-redo .tool {
    width: 38px;
    height: 38px;
  }

  .bar-right .button {
    width: 40px;
    height: 40px;
    padding: 0;
    justify-content: center;
  }

  .bar-right .button span {
    display: none;
  }

  .app[data-view] .pane-code,
  .app[data-view] .divider {
    display: none !important;
  }

  .app[data-view] .pane-canvas {
    display: flex !important;
  }

  .popover.history {
    position: fixed;
    left: 8px;
    right: 8px;
    top: calc(var(--bar-h) + 6px);
    width: auto;
  }

  .phone-bar {
    grid-area: phone;
    display: flex;
    height: calc(58px + env(safe-area-inset-bottom));
    padding: 4px 8px env(safe-area-inset-bottom);
    background: var(--surface);
    border-top: 1px solid var(--border);
  }

  .phone-bar button {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--text-faint);
    font-size: 10.5px;
    font-weight: 600;
    cursor: pointer;
  }

  .phone-bar button .icon {
    width: 20px;
    height: 20px;
  }

  .phone-bar button.on {
    color: var(--accent);
  }

  .sheet:not([hidden]) {
    display: flex;
    flex-direction: column;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(58px + env(safe-area-inset-bottom));
    z-index: 40;
    max-height: 58vh;
    background: var(--raised);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -12px 40px rgb(0 0 0 / 0.38);
  }

  .sheet-grip {
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: var(--border-strong);
    margin: 8px auto 2px;
  }

  .sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 8px 6px 16px;
    border-bottom: 1px solid var(--border);
  }

  .sheet-head b {
    font-size: 15px;
  }

  .sheet-sub {
    font-size: var(--t-11);
    color: var(--text-faint);
    margin-left: 8px;
    margin-right: auto;
  }

  .sheet-body {
    overflow: auto;
    padding: 8px;
  }

  .sheet-body .code-host {
    height: 44vh;
  }

  .insert-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    padding: 4px;
  }

  .insert-grid button {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 16px 8px;
    border: 0;
    border-radius: 12px;
    background: var(--surface-2);
    color: var(--text);
    font-weight: 500;
    cursor: pointer;
  }

  .insert-grid .icon {
    width: 22px;
    height: 22px;
    color: var(--accent);
  }
}
