*,
*::before,
*::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --border: #dadce0;
  --border-focus: #1a73e8;
  --text: #202124;
  --text-muted: #5f6368;
  --header-bg: #f8f9fa;
  --header-text: #5f6368;
  --selected-bg: #e8f0fe;
  --selected-border: #1a73e8;
  --active-cell: #1a73e8;
  --ribbon-bg: #dbdbdb;
  --tab-active: #ffffff;
  --row-header-width: 46px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-mono: "Consolas", "Cascadia Mono", monospace;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
html, body { height: 100%; overflow: hidden; }
body {
  font-family: var(--font); font-size: 13px; color: var(--text); background: var(--bg);
  touch-action: manipulation;
}

.app {
  display: flex; flex-direction: column;
  height: 100vh;
  height: 100dvh;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* Title bar — desktop: single row */
.title-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 4px 10px; background: #ffffff; border-bottom: 1px solid #dadce0; flex-shrink: 0;
  min-height: 52px; flex-wrap: nowrap;
}
.title-left {
  display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1;
  flex-wrap: nowrap; overflow-x: auto; scrollbar-width: thin;
}
.title-left::-webkit-scrollbar { height: 4px; }
.title-left::-webkit-scrollbar-thumb { background: #dadce0; border-radius: 2px; }
.app-logo-link {
  display: flex; align-items: center; flex-shrink: 0;
  text-decoration: none; line-height: 0;
}
.app-logo {
  display: block;
  height: 44px;
  width: auto;
  max-width: 200px;
  object-fit: contain;
  object-position: left center;
}
.title-meta {
  display: flex; align-items: center; gap: 4px; min-width: 0; flex-shrink: 0;
  padding-left: 10px; border-left: 1px solid #dadce0;
}
.file-name-input {
  width: 140px; min-width: 100px; max-width: 180px;
  padding: 4px 8px; border: 1px solid #dadce0; border-radius: 3px;
  background: #fff; color: var(--text); font-family: inherit; font-size: 12px;
  outline: none;
}
.file-name-input:focus { border-color: var(--border-focus); box-shadow: 0 0 0 1px var(--border-focus); }
.dirty-indicator { color: #ea4335; font-size: 18px; flex-shrink: 0; }
.title-refs {
  display: flex; align-items: center; gap: 8px; flex-wrap: nowrap; flex-shrink: 0;
  padding-left: 10px; border-left: 1px solid #dadce0;
}
.title-refs-heading {
  font-size: 11px; font-weight: 600; color: var(--text-muted); white-space: nowrap;
}
.ref-field {
  display: flex; align-items: center; gap: 4px;
  font-size: 11px; color: var(--text-muted); white-space: nowrap; flex-shrink: 0;
}
.ref-input {
  width: 72px; padding: 3px 5px; border: 1px solid #dadce0; border-radius: 3px;
  background: #fff; color: var(--text); font-family: inherit; font-size: 11px; outline: none;
}
.ref-input.ref-wide { width: 88px; }
.ref-input:focus { border-color: var(--border-focus); box-shadow: 0 0 0 1px var(--border-focus); }
.title-actions { display: flex; gap: 4px; flex-shrink: 0; flex-wrap: nowrap; align-items: center; }
.title-actions-group { display: flex; gap: 4px; flex-wrap: nowrap; flex-shrink: 0; }
.title-actions button {
  padding: 4px 8px; border: 1px solid #dadce0; border-radius: 4px;
  background: #fff; color: var(--text); font-family: inherit; font-size: 11px; cursor: pointer;
  white-space: nowrap;
}
.title-actions button:hover { background: #f1f3f4; }
.title-actions .btn-primary { background: #1a73e8; color: #fff; border-color: #1a73e8; }
.title-actions .btn-primary:hover { background: #1765cc; }
.title-actions .btn-danger { background: #fce8e6; color: #c5221f; border-color: #f5a8a0; }
.title-actions .btn-danger:hover { background: #fad2cf; }
.title-actions .btn-help { background: #1a73e8; color: #fff; border-color: #1a73e8; }
.title-actions .btn-help:hover { background: #1765cc; }

.mobile-only { display: none; }
.desktop-only { /* visible by default on desktop */ }

/* Mobile compact header */
.mobile-chrome-compact {
  display: none; flex-direction: column; flex-shrink: 0;
  background: #fff; border-bottom: 1px solid var(--border);
}
.mobile-header-logo {
  display: flex; justify-content: center; align-items: center;
  padding: 8px 12px 4px;
}
.mobile-header-logo .app-logo-link { display: flex; line-height: 0; }
.mobile-logo {
  height: 40px; width: auto; max-width: 200px; object-fit: contain;
}
.mobile-header-actions {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 10px 8px; min-height: 44px;
}
.mobile-header-meta {
  flex: 1; min-width: 0; display: flex; flex-direction: column;
  align-items: center; gap: 2px; text-align: center;
}
.mobile-compact-name {
  width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 13px; font-weight: 600;
}
.mobile-compact-cell {
  font-family: var(--font-mono); font-size: 11px; font-weight: 600;
  padding: 2px 8px; background: var(--header-bg); border-radius: 4px; color: var(--text-muted);
}
.mobile-compact-cell.has-stats {
  font-size: 12px; font-weight: 700; color: #0d47a1;
  background: #e8f0fe; border: 1px solid #aecbfa; padding: 3px 10px;
}
.btn-compact-open,
.btn-compact-save {
  flex-shrink: 0; padding: 8px 14px; border-radius: 6px;
  font-family: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
}
.btn-compact-open {
  border: 1px solid var(--border); background: #fff; color: var(--text);
}
.btn-compact-open:active { background: var(--selected-bg); }
.btn-compact-save {
  border: none; background: #1a73e8; color: #fff;
}
.btn-compact-save:active { background: #1765cc; }

.mobile-disclaimer {
  display: none; align-items: flex-start; gap: 10px;
  padding: 10px 12px; margin: 0;
  background: #fef7e0; border-bottom: 1px solid #f9e4a6;
  color: #5f4200; font-size: 12px; line-height: 1.45;
  position: relative; z-index: 150;
  flex-shrink: 0;
}
.mobile-disclaimer:not([hidden]):not(.is-dismissed) { display: flex; }
.mobile-disclaimer[hidden],
.mobile-disclaimer.is-dismissed { display: none !important; }
.mobile-disclaimer p { margin: 0; flex: 1; }
.mobile-disclaimer-dismiss {
  flex-shrink: 0; width: 44px; height: 44px; min-width: 44px; min-height: 44px; padding: 0;
  border: none; border-radius: 8px; background: rgba(0,0,0,0.06);
  color: #5f4200; font-size: 24px; line-height: 1; cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.mobile-disclaimer-dismiss:active { background: rgba(0,0,0,0.08); }

/* Mobile contextual format bar (shown while editing — sits below header, above keyboard) */
.mobile-edit-bar {
  display: none; position: fixed; left: 0; right: 0; top: 0; z-index: 200;
  align-items: center; gap: 6px; padding: 8px 10px;
  background: #fff; border-bottom: 1px solid var(--border);
  box-shadow: 0 2px 16px rgba(0,0,0,0.1);
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  touch-action: manipulation;
}
.mobile-edit-bar:not([hidden]) { display: flex; }
.mobile-edit-bar[hidden] { display: none !important; }
.mobile-fmt-btn {
  flex-shrink: 0; width: 44px; height: 44px; padding: 0;
  border: 1px solid var(--border); border-radius: 8px; background: #fff;
  font-family: inherit; font-size: 16px; cursor: pointer;
}
.mobile-fmt-btn.active { background: #d2e3fc; border-color: #1a73e8; color: #1a73e8; }
.mobile-fmt-btn:active { background: var(--selected-bg); }
.mobile-fmt-color {
  flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; border: 1px solid var(--border); border-radius: 8px;
  background: #fff; font-weight: 700; font-size: 15px; cursor: pointer;
}
.mobile-fmt-color input[type="color"] {
  position: absolute; opacity: 0; width: 44px; height: 44px; cursor: pointer;
}
.mobile-fmt-sep {
  flex-shrink: 0; width: 1px; height: 28px; background: var(--border); margin: 0 2px;
}
.mobile-fmt-done {
  flex-shrink: 0; margin-left: auto; padding: 0 18px; height: 44px;
  border: none; border-radius: 8px; background: #1a73e8; color: #fff;
  font-family: inherit; font-size: 15px; font-weight: 600; cursor: pointer;
}
.mobile-fmt-done:active { background: #1765cc; }

.app-mobile-minimal.editing-cell #gridWorkspace {
  padding-top: 52px;
}

/* Ribbon bar (tabs + mobile collapse) */
.ribbon-bar {
  display: flex; align-items: stretch; background: var(--ribbon-bg);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.ribbon-tabs {
  display: flex; gap: 0; flex: 1; min-width: 0; overflow-x: auto;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.ribbon-tabs::-webkit-scrollbar { display: none; }
.ribbon-toggle-btn {
  flex-shrink: 0; width: 40px; border: none; border-left: 1px solid var(--border);
  background: var(--ribbon-bg); color: var(--text-muted); font-size: 12px; cursor: pointer;
}
.ribbon-toggle-btn:hover { background: #e8eaed; }

/* Ribbon tabs */
.ribbon-tabs .ribbon-tab {
  padding: 6px 16px; border: none; background: transparent; font-family: inherit;
  font-size: 12px; cursor: pointer; color: var(--text-muted);
}
.ribbon-tab:hover { background: #e8eaed; }
.ribbon-tab.active { background: var(--tab-active); color: var(--text); font-weight: 500; border-bottom: 2px solid #1a73e8; }

/* Ribbon */
.ribbon {
  background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0; min-height: 72px;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
}
.ribbon.ribbon-collapsed { display: none; }
.ribbon-panel { display: none; padding: 6px 10px; gap: 12px; flex-wrap: nowrap; align-items: flex-start; width: max-content; min-width: 100%; }
.ribbon-panel.active { display: flex; }
.ribbon-group { display: flex; flex-direction: column; gap: 4px; padding-right: 12px; border-right: 1px solid var(--border); flex-shrink: 0; }
.ribbon-group:last-child { border-right: none; }
.ribbon-label { font-size: 10px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; white-space: nowrap; }
.ribbon-btns { display: flex; gap: 4px; flex-wrap: nowrap; align-items: center; }
.ribbon-btns button, .ribbon-btns select {
  padding: 4px 7px; border: 1px solid var(--border); border-radius: 3px;
  background: var(--surface); font-family: inherit; font-size: 12px; cursor: pointer;
  white-space: nowrap; flex-shrink: 0;
}
.ribbon-btns button:hover { background: var(--header-bg); }
.ribbon-btns button:disabled { opacity: 0.4; cursor: default; }
.ribbon-padding-label {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; color: var(--text-muted);
}
.ribbon-padding-label input {
  width: 44px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 3px;
  font-family: inherit; font-size: 12px;
}
.ribbon-inline-count {
  display: inline-flex; align-items: center; gap: 4px; flex-shrink: 0;
}
.ribbon-inline-count input[type="number"] {
  width: 48px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 3px;
  font-family: inherit; font-size: 12px; text-align: center;
}
.modal-hint { font-size: 13px; color: var(--text-muted); margin: 0 0 12px; line-height: 1.4; }
.add-rows-options { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.radio-label { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.toggle-btn.active { background: #d2e3fc; border-color: #1a73e8; }
.color-btn {
  display: inline-flex; align-items: center; gap: 2px; padding: 4px 6px;
  border: 1px solid var(--border); border-radius: 3px; cursor: pointer; font-size: 12px;
}
.color-btn input[type="color"] { width: 20px; height: 20px; border: none; padding: 0; cursor: pointer; }

.border-picker {
  position: absolute; z-index: 1000; background: #fff;
  border: 1px solid var(--border); border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15); padding: 10px;
  min-width: 148px;
}
.border-picker-label {
  font-size: 11px; color: var(--text-muted); margin-bottom: 8px; text-align: center;
}
.border-picker-grid {
  display: grid; grid-template-columns: repeat(3, 36px); gap: 4px;
  justify-content: center; margin-bottom: 8px;
}
.border-picker-spacer { width: 36px; height: 32px; }
.border-picker-presets {
  display: flex; flex-wrap: wrap; gap: 4px; justify-content: center;
  padding-top: 8px; border-top: 1px solid var(--border);
}
.border-picker-btn {
  width: 36px; height: 32px; padding: 0;
  border: 1px solid var(--border); border-radius: 4px;
  background: #fff; cursor: pointer; font-size: 14px; line-height: 1;
  color: var(--text);
}
.border-picker-presets .border-picker-btn {
  width: auto; min-width: 0; height: 28px; padding: 0 8px; font-size: 11px;
}
.border-picker-btn:hover { background: #e8f0fe; border-color: #1a73e8; }

/* Formula bar */
.formula-bar {
  display: flex; align-items: center; height: 32px; padding: 0 8px;
  background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.cell-ref {
  width: 80px; padding: 3px 6px; border: 1px solid var(--border); border-radius: 2px;
  background: var(--header-bg); font-family: var(--font-mono); font-size: 12px; text-align: center;
}
.formula-separator { margin: 0 8px; color: var(--text-muted); font-style: italic; font-size: 12px; }
.formula-input {
  flex: 1; padding: 3px 8px; border: 1px solid transparent; border-radius: 2px;
  font-family: var(--font-mono); font-size: 13px; outline: none;
}
.formula-input:focus { border-color: var(--border-focus); }

.grid-sheet-area {
  flex: 1; min-height: 0; position: relative; display: flex; overflow: hidden;
}

/* Sheet grid */
.sheet-container {
  flex: 1; min-width: 0; min-height: 0;
  overflow: auto; background: #e8eaed; position: relative;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  overflow-anchor: none;
  touch-action: pan-x pan-y;
  scrollbar-width: thin;
  scrollbar-color: #9aa0a6 #e8eaed;
}
.sheet-container::-webkit-scrollbar {
  width: 14px;
  height: 14px;
}
.sheet-container::-webkit-scrollbar-thumb {
  background: #9aa0a6;
  border-radius: 8px;
  border: 3px solid #e8eaed;
}
.sheet-container::-webkit-scrollbar-thumb:hover {
  background: #80868b;
}
.sheet-container::-webkit-scrollbar-track {
  background: #e8eaed;
}
.sheet-container::-webkit-scrollbar-corner {
  background: #e8eaed;
}
.sheet-zoom-wrapper { display: block; transform-origin: 0 0; vertical-align: top; }
.sheet-scroll-content {
  position: relative; background: var(--surface); overflow-anchor: none;
}
.scroll-height-spacer {
  display: block; margin: 0; padding: 0; border: 0;
  pointer-events: none; visibility: hidden;
}

.sheet-grid-frame {
  position: absolute; top: 0; left: 0;
}

.sheet { border-collapse: collapse; table-layout: fixed; user-select: none; width: auto; }
.sheet thead { position: relative; z-index: 3; }
.sheet th, .sheet td {
  border: 1px solid var(--border); padding: 0; vertical-align: middle;
  box-sizing: border-box;
}

.corner-cell {
  width: var(--row-header-width); min-width: var(--row-header-width); height: 24px;
  background: var(--header-bg); position: sticky; top: 0; left: 0; z-index: 4;
}

.col-header {
  height: 24px; background: var(--header-bg); color: var(--header-text);
  font-size: 11px; font-weight: 500; text-align: center;
  position: sticky; top: 0; z-index: 3;
  cursor: grab; user-select: none;
}
.col-header.selected { background: #c2d7f5; color: #1a3a5c; font-weight: 700; }
.col-header.unused { background: #fff3e0; color: #bf360c; }
.col-header.unused.selected { background: #ffcc80; color: #4e2600; }
.col-header.locked { background: #fef7e0; color: #8b6914; }
.col-header.locked.selected { background: #fde293; }
.col-header.locked::after {
  content: ' 🔒'; font-size: 9px;
}
.col-header span { pointer-events: none; }
.col-resize {
  position: absolute; right: 0; top: 0; width: 5px; height: 100%;
  cursor: col-resize; z-index: 5;
}

.row-header {
  width: var(--row-header-width); min-width: var(--row-header-width);
  background: var(--header-bg); color: var(--header-text);
  font-size: 11px; font-weight: 500; text-align: center;
  position: sticky; left: 0; z-index: 1;
  cursor: grab; user-select: none;
}
.row-header.selected { background: #c2d7f5; color: #1a3a5c; font-weight: 700; }
.row-header.unused { background: #fff3e0; color: #bf360c; }
.row-header.unused.selected { background: #ffcc80; color: #4e2600; }
.row-header.locked { background: #fef7e0; color: #8b6914; }
.row-header.locked.selected { background: #fde293; }
.row-header.locked::after {
  content: '🔒'; font-size: 9px; display: block; line-height: 1; margin-top: 1px;
}

.cell {
  padding: 0 4px; font-size: 13px; overflow: hidden; cursor: cell; background: var(--surface);
  position: relative;
}
.cell.in-range { background: var(--selected-bg); }
.cell.cell-locked:not(.in-range) { background: #fafafa; color: #5f6368; }
.cell.in-range.cell-locked { background: #e8f0fe; }
.cell.has-dropdown,
.cell.has-date { padding-right: 20px; }
.cell-display-text {
  display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding-right: 2px;
}
.cell-link {
  color: #1a73e8; text-decoration: underline; cursor: pointer;
}
.cell-link:hover { color: #174ea6; }
.cell-dropdown-btn,
.cell-date-btn {
  position: absolute; right: 0; top: 0; bottom: 0; width: 20px;
  border: none; border-left: 1px solid transparent; background: transparent;
  cursor: pointer; font-size: 11px; color: #5f6368; padding: 0;
  display: flex; align-items: center; justify-content: center;
}
.cell.in-range .cell-dropdown-btn,
.cell.in-range .cell-date-btn { border-left-color: #c6dafc; }
.cell-dropdown-btn:hover,
.cell-date-btn:hover { background: #d2e3fc; color: #1a73e8; }
.cell-dropdown-picker {
  position: fixed; z-index: 10000; min-width: 120px; max-width: 280px;
  border: 1px solid var(--border-focus); border-radius: 4px;
  background: #fff; box-shadow: 0 4px 16px rgba(0,0,0,0.18);
  font-family: inherit; font-size: 13px; outline: none;
}
.cell.merge-hidden {
  background: var(--selected-bg);
  border-color: transparent;
  color: transparent;
  pointer-events: none;
}
.cell.editing {
  padding: 0;
  background: var(--surface);
  box-shadow: inset 0 0 0 2px var(--active-cell);
  z-index: 2;
  user-select: text;
  -webkit-user-select: text;
  touch-action: auto;
}

.cell-input {
  width: 100%; height: 100%; border: none; outline: none;
  padding: 0 4px; font-family: inherit; font-size: 13px; background: transparent;
  user-select: text;
  -webkit-user-select: text;
  touch-action: auto;
  -webkit-touch-callout: default;
  color: inherit;
}

.selection-overlay {
  position: absolute; pointer-events: none;
  outline: 2px solid var(--selected-border); z-index: 10;
  box-sizing: border-box;
}
.selection-overlay-rows,
.selection-overlay-cols {
  background: rgba(232, 240, 254, 0.45);
}

.reorder-indicator {
  position: absolute; z-index: 15; pointer-events: none;
  background: #1a73e8; box-shadow: 0 0 6px rgba(26, 115, 232, 0.6);
}
.reorder-indicator-row { height: 3px; left: 0; right: 0; }
.reorder-indicator-col { width: 3px; top: 0; bottom: 0; }
body.header-reorder-active { cursor: grabbing !important; }
body.header-reorder-active .row-header,
body.header-reorder-active .col-header { cursor: grabbing; }
body.header-reorder-active .row-header.reorder-source,
body.header-reorder-active .col-header.reorder-source {
  opacity: 0.35; pointer-events: none;
}
body.header-reorder-active tr.reorder-source-row td.cell,
body.header-reorder-active tr.reorder-source-row th.row-header {
  opacity: 0.2;
  background: #e8eaed !important;
  color: #9aa0a6 !important;
}
body.header-reorder-active th.col-header.reorder-source-col,
body.header-reorder-active td.cell.reorder-source-col {
  opacity: 0.2;
  background: #e8eaed !important;
  color: #9aa0a6 !important;
}
.reorder-ghost {
  position: fixed; z-index: 9999; pointer-events: none;
  opacity: 0.94;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.22), 0 0 0 2px #1a73e8;
  border-radius: 4px;
  background: var(--surface);
  overflow: hidden;
  transform-origin: 0 0;
}
.reorder-ghost-table {
  border-collapse: separate; border-spacing: 0;
  table-layout: fixed; user-select: none;
}
.reorder-ghost-table th,
.reorder-ghost-table td {
  border: 1px solid var(--border);
  padding: 0; vertical-align: middle;
  overflow: hidden;
}
.reorder-ghost .cell-dropdown-btn,
.reorder-ghost .cell-date-btn { display: none; }
.row-header.reorder-drop-target,
.col-header.reorder-drop-target {
  background: #1a73e8 !important;
  color: #fff !important;
  font-weight: 700;
  box-shadow: inset 0 0 0 2px #174ea6;
  z-index: 12;
}
tr.reorder-drop-row .cell {
  box-shadow: inset 0 0 0 2px #1a73e8;
}
th.col-header.reorder-drop-col {
  box-shadow: inset 0 0 0 2px #174ea6;
}

.charts-layer { position: absolute; top: 0; left: 0; right: 0; bottom: 0; pointer-events: none; z-index: 20; }
.chart-widget {
  position: absolute; pointer-events: auto; background: #fff;
  border: 1px solid var(--border); border-radius: 6px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.15); overflow: hidden;
}
.chart-toolbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 4px 8px; background: var(--header-bg); font-size: 12px; font-weight: 500; cursor: move;
}
.chart-del { border: none; background: none; font-size: 18px; cursor: pointer; color: #5f6368; line-height: 1; }
.chart-del:hover { color: #c5221f; }
.chart-widget canvas { display: block; }

.frozen-row th, .frozen-row td { position: sticky; top: 24px; z-index: 2; background: var(--header-bg); }
.frozen-col { position: sticky; left: var(--row-header-width); z-index: 2; }

/* Sheet tabs */
.sheet-tabs-bar {
  display: flex; align-items: center; height: 32px; background: var(--header-bg);
  border-top: 1px solid var(--border); flex-shrink: 0; overflow: hidden;
}
.tab-add {
  width: 32px; height: 100%; border: none; background: transparent; cursor: pointer;
  font-size: 16px; color: var(--text-muted); flex-shrink: 0;
}
.tab-add:hover { background: #e8eaed; }
.sheet-tabs { display: flex; overflow-x: auto; flex: 1; }
.sheet-tab {
  padding: 0 16px; height: 100%; border: none; border-right: 1px solid var(--border);
  background: transparent; font-family: inherit; font-size: 12px; cursor: pointer;
  white-space: nowrap; color: var(--text-muted);
}
.sheet-tab:hover { background: #e8eaed; }
.sheet-tab.active { background: var(--surface); color: var(--text); font-weight: 500; border-top: 2px solid #1a73e8; }

/* Status bar */
.status-bar {
  display: flex; justify-content: space-between; align-items: center; gap: 16px;
  min-height: 28px; padding: 2px 12px; padding-bottom: env(safe-area-inset-bottom);
  background: var(--header-bg);
  border-top: 1px solid var(--border); font-size: 11px; color: var(--text-muted); flex-shrink: 0;
}
.status-owner { margin-left: auto; white-space: nowrap; font-size: 10px; }
.selection-stats {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 600;
  color: #174ea6;
  white-space: nowrap;
  padding: 4px 14px;
  background: linear-gradient(180deg, #f0f6ff 0%, #e8f0fe 100%);
  border: 1px solid #aecbfa;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(26, 115, 232, 0.18);
}
.selection-stats-total {
  font-size: 14px;
  font-weight: 800;
  color: #1a73e8;
  letter-spacing: 0.01em;
}
.selection-stats-detail {
  font-size: 12px;
  font-weight: 600;
  color: #3c4043;
  padding-left: 10px;
  border-left: 1px solid #c6dafc;
}
.selection-stats[hidden] { display: none !important; }

/* Context menu */
.context-menu {
  position: fixed; z-index: 1000; background: var(--surface);
  border: 1px solid var(--border); border-radius: 4px; box-shadow: 0 2px 10px rgba(0,0,0,0.15);
  min-width: 180px; padding: 4px 0; list-style: none;
}
.context-menu button {
  display: block; width: 100%; padding: 6px 16px; border: none; background: none;
  text-align: left; font-family: inherit; font-size: 13px; cursor: pointer;
}
.context-menu button:hover { background: var(--selected-bg); }
.context-menu hr { border: none; border-top: 1px solid var(--border); margin: 4px 0; }

/* Modal */
.modal {
  border: none; border-radius: 8px; padding: 20px; box-shadow: 0 4px 20px rgba(0,0,0,0.2);
  min-width: min(360px, calc(100vw - 32px));
  max-width: calc(100vw - 16px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
}
dialog.modal-help[open] {
  display: flex;
  flex-direction: column;
}

/* Help modal */
.modal-help {
  width: min(920px, 92vw);
  height: min(80vh, 720px);
  max-width: 92vw;
  max-height: 80vh;
  padding: 0;
  overflow: hidden;
}
.help-modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; background: #1a3a5c; color: #fff; flex-shrink: 0;
}
.help-modal-header h2 { font-size: 16px; margin: 0; color: #fff; }
.modal-close {
  width: 32px; height: 32px; border: none; border-radius: 4px;
  background: rgba(255,255,255,0.15); color: #fff; font-size: 22px;
  line-height: 1; cursor: pointer;
}
.modal-close:hover { background: rgba(255,255,255,0.25); }
.help-modal-body {
  display: flex; flex: 1; min-height: 0; overflow: hidden;
}
.help-toc {
  width: 180px; flex-shrink: 0; padding: 12px 8px; background: var(--header-bg);
  border-right: 1px solid var(--border); overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px;
}
.help-toc button {
  text-align: left; padding: 8px 10px; border: none; border-radius: 4px;
  background: transparent; font-family: inherit; font-size: 12px; cursor: pointer; color: var(--text);
}
.help-toc button:hover { background: #e8eaed; }
.help-toc button.active { background: var(--selected-bg); color: var(--border-focus); font-weight: 500; }
.help-content {
  flex: 1; overflow-y: auto; padding: 20px 28px 32px; line-height: 1.6; font-size: 14px;
}
.help-content h2 { font-size: 18px; margin-bottom: 10px; color: #1a3a5c; }
.help-content h3 { font-size: 14px; margin: 18px 0 6px; color: var(--text); }
.help-content p { margin-bottom: 10px; }
.help-content ul, .help-content ol { margin: 0 0 12px 20px; }
.help-content li { margin-bottom: 4px; }
.help-content code, .help-content kbd {
  background: var(--header-bg); padding: 1px 5px; border-radius: 3px;
  font-family: var(--font-mono); font-size: 12px; border: 1px solid var(--border);
}
.help-table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 13px; }
.help-table th, .help-table td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.help-table th { background: var(--header-bg); font-weight: 600; }
.help-footer { margin-top: 20px; padding-top: 14px; border-top: 1px solid var(--border); font-size: 12px; color: var(--text-muted); }

.modal h2 { font-size: 16px; margin-bottom: 12px; }
.val-selection-label {
  font-size: 12px; color: #1a73e8; background: #e8f0fe;
  padding: 6px 10px; border-radius: 4px; margin-bottom: 10px;
}
.val-hint { font-size: 12px; color: var(--text-muted); margin-top: 8px; }
.band-color-picks {
  display: flex; flex-wrap: wrap; gap: 16px; margin: 12px 0;
}
.band-color-picks label { flex: 1; min-width: 140px; }
.band-color-picks input[type="color"] {
  display: block; width: 100%; height: 36px; margin-top: 4px; padding: 2px; cursor: pointer;
}
.band-preview {
  display: flex; height: 28px; border-radius: 4px; overflow: hidden;
  border: 1px solid var(--border); margin-bottom: 8px;
}
.band-preview span { flex: 1; }
.pdf-user-section {
  margin-top: 12px; padding: 10px 12px; background: #f8f9fa;
  border: 1px solid var(--border); border-radius: 6px;
}
.pdf-section-title { font-size: 12px; font-weight: 600; margin-bottom: 8px; color: var(--text); }
.pdf-user-section label { margin-bottom: 6px; }
.pdf-omit-meta { margin-top: 8px; margin-bottom: 0 !important; }
.pdf-ref-hint { font-size: 11px; color: var(--text-muted); margin-top: 10px; line-height: 1.4; }
.modal label { display: block; margin-bottom: 8px; font-size: 13px; }
.modal input[type="text"], .modal select, .modal textarea {
  width: 100%; padding: 6px 8px; margin-top: 4px; border: 1px solid var(--border);
  border-radius: 4px; font-family: inherit;
}
.warning-text { color: #c5221f; margin-bottom: 12px; line-height: 1.5; }
.hint-text { font-size: 12px; color: var(--text-muted); margin-bottom: 12px; }
.restore-info { font-weight: 500; margin: 8px 0 16px; }
.autosave-status { color: #188038; font-size: 11px; }
.modal .btn-primary { background: #1a73e8; color: #fff; border-color: #1a73e8; }
.modal .btn-danger { background: #c5221f; color: #fff; border-color: #c5221f; }
.cell-select { appearance: auto; }

#gridWorkspace { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow: hidden; }
.checkbox-label { display: flex; align-items: center; gap: 6px; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; justify-content: flex-end; }
.modal-actions button {
  padding: 6px 14px; border: 1px solid var(--border); border-radius: 4px;
  background: var(--surface); font-family: inherit; cursor: pointer;
}

/* Mobile toolbar — removed; kept rule to suppress legacy markup */
.mobile-toolbar { display: none !important; }

.mobile-scroll-pad { display: none !important; }

/* Mobile overlay scrollbars (large touch targets) */
.mobile-scroll-v,
.mobile-scroll-h {
  display: none; position: absolute; z-index: 50;
  pointer-events: auto; touch-action: none;
}
.mobile-scroll-v {
  right: 0; top: 0; bottom: 40px; width: 44px; padding: 8px 6px;
}
.mobile-scroll-h {
  left: 0; right: 40px; bottom: 0; height: 44px; padding: 6px 8px;
}
.mobile-scroll-v-track,
.mobile-scroll-h-track {
  width: 100%; height: 100%; background: rgba(0, 0, 0, 0.12);
  border-radius: 18px; position: relative; touch-action: none;
}
.mobile-scroll-v-thumb,
.mobile-scroll-h-thumb {
  position: absolute; left: 0; top: 0;
  width: 100%; min-height: 56px;
  background: #5f6368; border-radius: 14px;
  touch-action: none; cursor: grab; box-shadow: 0 1px 4px rgba(0,0,0,0.25);
  -webkit-user-select: none; user-select: none;
}
.mobile-scroll-v-thumb.dragging,
.mobile-scroll-h-thumb.dragging {
  background: #1a73e8; cursor: grabbing; z-index: 2;
}
.mobile-scroll-h-thumb {
  top: 0; height: 100%; min-height: 0; min-width: 56px;
}
.mobile-scroll-v-thumb:active,
.mobile-scroll-h-thumb:active { background: #1a73e8; cursor: grabbing; }
.mobile-scroll-v.scroll-disabled,
.mobile-scroll-h.scroll-disabled { opacity: 0.35; pointer-events: none; }

.btn-compact-pan,
.btn-compact-toggle,
.btn-compact-file,
.mobile-file-menu,
.mobile-scroll-float { display: none !important; }

/* Mobile compact chrome bar — see top of file */

.sheet-container.sheet-pan-mode {
  cursor: grab; touch-action: none;
}
.sheet-container.sheet-pan-mode.sheet-pan-dragging { cursor: grabbing; }
.touch-device.sheet-pan-mode .sheet,
.touch-device.sheet-pan-mode .charts-layer,
.touch-device.sheet-pan-mode .selection-overlay {
  pointer-events: none;
}

/* Touch-friendly targets */
@media (hover: none) and (pointer: coarse) {
  .touch-device .cell,
  .touch-device .row-header,
  .touch-device .col-header {
    touch-action: none;
  }
  .title-actions button,
  .ribbon-tabs .ribbon-tab,
  .ribbon-btns button,
  .ribbon-btns select,
  .context-menu button,
  .sheet-tab,
  .tab-add,
  .mobile-toolbar button {
    min-height: 44px;
  }
  .mobile-fmt-btn,
  .mobile-fmt-done,
  .mobile-fmt-color,
  .btn-compact-open,
  .btn-compact-save {
    min-height: 44px; min-width: 44px;
  }
  .ribbon-btns button,
  .title-actions button:not(.btn-mobile-menu) {
    min-width: 44px;
    padding: 8px 12px;
  }
  .cell-dropdown-btn,
  .cell-date-btn {
    width: 36px; min-width: 36px; font-size: 14px;
  }
  .cell.has-dropdown,
  .cell.has-date { padding-right: 36px; }
  .col-resize { width: 14px; }
  .context-menu button { padding: 12px 16px; font-size: 15px; }
  .context-menu { max-height: min(70dvh, 480px); overflow-y: auto; }
}

/* Tablet / phone — minimal read + edit mode */
@media (max-width: 960px) {
  .desktop-only { display: none !important; }
  .mobile-chrome-compact { display: flex; }
  .mobile-only:not(.mobile-disclaimer) { display: flex; }
  .mobile-disclaimer.mobile-only:not([hidden]):not(.is-dismissed) { display: flex; }

  .sheet-container {
    scrollbar-width: none;
  }
  .sheet-container::-webkit-scrollbar { display: none; }

  .mobile-scroll-v.mobile-only { display: block; }
  .mobile-scroll-h.mobile-only { display: block; }

  .app-mobile-minimal .status-bar {
    height: 24px; padding: 0 10px; font-size: 11px;
  }
  .app-mobile-minimal #autosaveStatus,
  .app-mobile-minimal #selectionStats,
  .app-mobile-minimal #cellCount,
  .app-mobile-minimal .status-owner { display: none; }

  /* Charts view-only on mobile */
  .app-mobile-minimal .chart-widget { pointer-events: none; }
  .app-mobile-minimal .chart-toolbar .chart-del { display: none; }

  .help-modal-body { flex-direction: column; }
  .help-toc {
    width: 100%; flex-direction: row; flex-wrap: nowrap; overflow-x: auto;
    border-right: none; border-bottom: 1px solid var(--border);
    -webkit-overflow-scrolling: touch;
  }
  .help-toc button { white-space: nowrap; flex-shrink: 0; }
  .help-content { padding: 16px 18px 24px; }
}

/* Phone */
@media (max-width: 600px) {
  .app-logo { height: 40px; max-width: 160px; }
  .title-bar { min-height: 0; }
  .file-name-input { width: 100%; max-width: none; min-width: 0; }
  .title-refs-heading { width: 100%; }
  .ref-field { flex: 1 1 calc(50% - 4px); min-width: 140px; }
  .formula-bar {
    flex-wrap: wrap; height: auto; min-height: 32px; padding: 4px 6px; gap: 4px;
  }
  .cell-ref { width: 56px; flex-shrink: 0; }
  .formula-separator { margin: 0 4px; }
  .formula-input { min-width: 0; flex: 1 1 160px; font-size: 16px; }
  .modal { padding: 16px; margin: auto; }
  .modal-actions { flex-wrap: wrap; justify-content: stretch; }
  .modal-actions button { flex: 1 1 auto; min-height: 44px; }
  .status-bar {
    height: auto; min-height: 24px; flex-wrap: wrap; padding: 4px 8px;
    padding-bottom: max(4px, env(safe-area-inset-bottom));
  }
  .selection-stats {
    order: 10;
    width: 100%;
    justify-content: center;
    font-size: 13px;
    padding: 6px 12px;
    margin-top: 2px;
  }
  .selection-stats-total { font-size: 15px; }
  .ribbon { min-height: 0; }
  .ribbon-panel { padding: 8px 10px; flex-wrap: wrap; width: auto; min-width: 0; }
  .ribbon-btns { flex-wrap: wrap; }
  .cell-input { font-size: 16px; }
  .cell-select { font-size: 16px; }
}

/* Print */
@media print {
  .title-bar, .ribbon-bar, .ribbon, .mobile-chrome-compact, .mobile-edit-bar, .mobile-scroll-v, .mobile-scroll-h, .formula-bar, .sheet-tabs-bar, .status-bar, .selection-overlay { display: none !important; }
  #gridWorkspace { display: block !important; }
  .sheet-container { overflow: visible; height: auto; }
  .cell { border: 1px solid #ccc !important; }
}
