*,
*::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 */
.title-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 4px 12px; background: #ffffff; border-bottom: 1px solid #dadce0; flex-shrink: 0;
  min-height: 60px;
}
.title-left { display: flex; align-items: center; gap: 14px; min-width: 0; }
.app-logo-link {
  display: flex; align-items: center; flex-shrink: 0;
  text-decoration: none; line-height: 0;
}
.app-logo {
  display: block;
  height: 52px;
  width: auto;
  max-width: 240px;
  object-fit: contain;
  object-position: left center;
}
.title-meta {
  display: flex; align-items: center; gap: 6px; min-width: 0;
  padding-left: 14px; border-left: 1px solid #dadce0;
}
.file-name-input {
  min-width: 120px; max-width: 280px; width: 200px;
  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; }
.title-refs {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-width: 0;
  padding-left: 14px; 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;
}
.ref-input {
  width: 88px; padding: 3px 6px; 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: 108px; }
.ref-input:focus { border-color: var(--border-focus); box-shadow: 0 0 0 1px var(--border-focus); }
.title-actions { display: flex; gap: 6px; flex-shrink: 0; flex-wrap: wrap; }
.title-actions-group { display: flex; gap: 6px; flex-wrap: wrap; }
.title-actions button {
  padding: 5px 12px; border: 1px solid #dadce0; border-radius: 4px;
  background: #fff; color: var(--text); font-family: inherit; font-size: 12px; cursor: pointer;
}
.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; }

/* 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 12px; gap: 16px; flex-wrap: wrap; align-items: flex-start; }
.ribbon-panel.active { display: flex; }
.ribbon-group { display: flex; flex-direction: column; gap: 4px; padding-right: 16px; border-right: 1px solid var(--border); }
.ribbon-group:last-child { border-right: none; }
.ribbon-label { font-size: 10px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.ribbon-btns { display: flex; gap: 4px; flex-wrap: wrap; align-items: center; }
.ribbon-btns button, .ribbon-btns select {
  padding: 4px 8px; border: 1px solid var(--border); border-radius: 3px;
  background: var(--surface); font-family: inherit; font-size: 12px; cursor: pointer;
}
.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;
}
.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); }

/* Sheet grid */
.sheet-container {
  flex: 1; overflow: auto; background: #e8eaed; position: relative;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  touch-action: pan-x pan-y;
}
.scroll-spacer { position: absolute; top: 0; left: 0; pointer-events: none; visibility: hidden; }
.sheet-zoom-wrapper { position: absolute; top: 0; left: 0; }
.sheet-wrapper { display: inline-block; position: relative; background: var(--surface); }

.sheet { border-collapse: separate; border-spacing: 0; table-layout: fixed; user-select: none; }
.sheet th, .sheet td { border: 1px solid var(--border); padding: 0; vertical-align: middle; }

.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: 2;
  position: relative; cursor: pointer; user-select: none;
}
.col-header.selected { background: #c2d7f5; color: #1a3a5c; font-weight: 700; }
.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: pointer; user-select: none;
}
.row-header.selected { background: #c2d7f5; color: #1a3a5c; font-weight: 700; }
.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;
}

.cell-input {
  width: 100%; height: 100%; border: none; outline: none;
  padding: 0 4px; font-family: inherit; font-size: inherit; background: transparent;
}

.selection-overlay {
  position: absolute; pointer-events: none;
  border: 2px solid var(--selected-border); z-index: 10;
  box-sizing: border-box;
}

.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;
  height: 24px; padding: 0 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; }

/* 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; }
.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 (quick actions above grid) */
.mobile-toolbar {
  display: none; flex-shrink: 0; gap: 6px; padding: 6px 8px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  overflow-x: auto; -webkit-overflow-scrolling: touch;
}
.mobile-toolbar button {
  flex: 1; min-width: 72px; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--surface); font-family: inherit;
  font-size: 13px; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.mobile-toolbar button:active { background: var(--selected-bg); }

/* Touch-friendly targets */
@media (hover: none) and (pointer: coarse) {
  .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;
  }
  .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 */
@media (max-width: 960px) {
  .title-bar {
    flex-wrap: wrap; align-items: flex-start; gap: 8px;
    padding: 8px 10px;
  }
  .title-left {
    flex: 1 1 100%; flex-wrap: wrap; gap: 10px;
  }
  .title-meta {
    flex: 1 1 auto; border-left: none; padding-left: 0;
  }
  .title-refs {
    flex: 1 1 100%; border-left: none; padding-left: 0; gap: 8px;
  }
  .ref-input { flex: 1; min-width: 72px; width: auto; }
  .ref-input.ref-wide { width: auto; flex: 1.2; }
  .title-actions {
    flex: 1 1 100%; position: relative;
  }
  .mobile-only { display: flex; }
  .mobile-toolbar.mobile-only { display: flex; }
  .btn-mobile-menu {
    position: absolute; right: 0; top: 0; z-index: 2;
    width: 44px; height: 44px; padding: 0; justify-content: center; align-items: center;
    font-size: 20px; line-height: 1;
  }
  .title-actions .title-actions-group {
    display: none; flex-wrap: wrap; gap: 6px; width: 100%;
    padding-top: 48px;
  }
  .title-actions.title-actions-open .title-actions-group {
    display: flex;
    background: #fff; border: 1px solid var(--border); border-radius: 8px;
    padding: 8px; box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  }
  .status-owner { display: none; }
  #cellCount { display: none; }
  .ribbon-panel { flex-wrap: nowrap; width: max-content; min-width: 100%; }
  .ribbon-group { flex-shrink: 0; }
  .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));
  }
  #selectionStats { display: none; }
  .ribbon { min-height: 0; }
  .ribbon-panel { padding: 8px 10px; }
  .cell-input { font-size: 16px; }
  .cell-select { font-size: 16px; }

/* Print */
@media print {
  .title-bar, .ribbon-bar, .ribbon, .mobile-toolbar, .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; }
}
