/* ── Reset & base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e1e4e8;
  --border-light: #f0f1f3;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --text-faint: #9ca3af;
  --accent: #4f6ef7;
  --accent-hover: #3b5adb;
  --accent-light: #eef0fe;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --success: #22c55e;
  --success-light: #f0fdf4;
  --warning: #f59e0b;
  --warning-light: #fffbeb;
  --separator-bg: #f8f9fa;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.08);
  --shadow-md: 0 4px 16px rgba(0,0,0,.12);
  --radius: 8px;
  --header-h: 52px;
  --tabs-h: 44px;
  --toolbar-h: 48px;
  --controls-h: 52px;
  --sticky-top: calc(var(--header-h) + var(--tabs-h) + var(--toolbar-h) + var(--controls-h));
}

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); font-size: 13px; }

.hidden { display: none !important; }

/* ── Auth overlay ── */
#auth-overlay {
  position: fixed; inset: 0; background: var(--bg);
  display: flex; align-items: center; justify-content: center; z-index: 999;
}
#auth-box {
  background: var(--surface); border-radius: 16px; padding: 48px 56px;
  text-align: center; box-shadow: var(--shadow-md);
  display: flex; flex-direction: column; gap: 16px; align-items: center;
}
#auth-box h2 { font-size: 22px; font-weight: 600; }
#auth-box p { color: var(--text-muted); }

/* ── Buttons ── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 16px; border-radius: 6px; border: none;
  font-size: 13px; font-weight: 500; cursor: pointer; transition: background .15s, opacity .15s;
  white-space: nowrap;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary   { background: var(--accent); color: #fff; }
.btn-primary:not(:disabled):hover { background: var(--accent-hover); }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover { background: #d1d5db; }
.btn-ghost     { background: transparent; color: var(--accent); padding: 5px 8px; }
.btn-ghost:hover { background: var(--accent-light); }
.btn-icon      { background: transparent; border: none; cursor: pointer; font-size: 20px; color: var(--text-muted); line-height: 1; padding: 2px 6px; border-radius: 4px; }
.btn-icon:hover { background: var(--border); }

/* ── App shell ── */
#app { display: flex; flex-direction: column; height: 100%; }

/* ── Header ── */
#header {
  height: var(--header-h); background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; padding: 0 20px; gap: 12px;
  justify-content: space-between;
  position: sticky; top: 0; z-index: 40; box-shadow: var(--shadow-sm);
}
#app-title { font-size: 16px; font-weight: 600; color: var(--text); }
#header-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
#user-email { font-size: 12px; color: var(--text-muted); }
#logout-btn { padding: 5px 10px; font-size: 12px; }
#settings-wrap { position: relative; }
#settings-btn { color: var(--text-muted); }
#settings-btn:hover { color: var(--text); background: var(--border); }
#settings-menu {
  position: absolute; top: calc(100% + 4px); right: 0; z-index: 300;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-md);
  min-width: 200px; padding: 4px 0;
}
.settings-item { padding: 8px 16px; font-size: 13px; color: var(--text); }
.settings-item-placeholder { color: var(--text-muted); cursor: default; user-select: none; }

/* ── Tabs ── */
#tabs-bar {
  height: var(--tabs-h); background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: var(--header-h); z-index: 39;
  overflow-x: auto; overflow-y: hidden;
}
#tabs-bar::-webkit-scrollbar { height: 3px; }
#tabs-bar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

#tabs { display: flex; align-items: stretch; height: 100%; padding: 0 12px; gap: 2px; }
.tab {
  display: flex; align-items: center; padding: 0 14px;
  border: none; background: transparent; color: var(--text-muted);
  font-size: 13px; font-weight: 500; cursor: pointer; white-space: nowrap;
  border-bottom: 2px solid transparent; transition: color .15s, border-color .15s;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ── Toolbar ── */
#toolbar {
  height: var(--toolbar-h); background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; padding: 0 16px;
  position: sticky; top: calc(var(--header-h) + var(--tabs-h)); z-index: 38;
}

/* ── Controls ── */
#controls {
  height: var(--controls-h); background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 16px; padding: 0 16px;
  position: sticky; top: calc(var(--header-h) + var(--tabs-h) + var(--toolbar-h)); z-index: 37;
}

#search-wrap {
  position: relative; display: flex; align-items: center;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
  padding: 0 10px; gap: 8px; min-width: 240px; flex: 0 0 auto;
}
.search-icon { width: 15px; height: 15px; color: var(--text-faint); flex-shrink: 0; }
#search-input {
  border: none; background: transparent; outline: none;
  font-size: 13px; color: var(--text); width: 220px;
}
#search-clear {
  background: none; border: none; cursor: pointer; color: var(--text-faint);
  font-size: 16px; line-height: 1; padding: 2px 4px;
}
#search-clear:hover { color: var(--text); }

/* ── Tag filter ── */
#tag-filter-wrap { position: relative; }
#tag-filter-btn { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
#tag-filter-btn svg { opacity: 0.6; }
#tag-filter-popup {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 300;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-md);
  min-width: 180px; padding: 4px 0;
}
.tag-filter-item {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 14px; cursor: pointer; font-size: 13px; color: var(--text);
  user-select: none;
}
.tag-filter-item:hover { background: var(--accent-light); }
.tag-filter-item input[type="checkbox"] { cursor: pointer; flex-shrink: 0; }
.tag-filter-separator { border-top: 1px solid var(--border); margin: 4px 0; }
#tag-filter-reset { padding: 7px 14px; font-size: 13px; color: var(--accent); cursor: pointer; }
#tag-filter-reset:hover { background: var(--accent-light); }

/* ── Table container ── */
#table-container {
  flex: 1; overflow: auto; position: relative;
}

#data-table {
  border-collapse: collapse; width: max-content; min-width: 100%;
  background: var(--surface);
}

/* Header row */
#table-head tr th {
  position: sticky; top: 0; z-index: 10;
  background: var(--bg); border-bottom: 2px solid var(--border); border-right: 1px solid var(--border-light);
  padding: 8px 10px; text-align: left; font-weight: 600; font-size: 12px;
  color: var(--text-muted); white-space: nowrap; letter-spacing: .4px; text-transform: uppercase;
  user-select: none;
}
#table-head tr th:last-child { border-right: none; }

/* Data rows */
#table-body tr { border-bottom: 1px solid var(--border-light); }
#table-body tr:hover td { background: #f8f9ff; }
#table-body tr:hover td.sep-col { background: #eff0f2; }

#table-body td {
  padding: 7px 10px; vertical-align: top;
  border-right: 1px solid var(--border-light);
  max-width: 300px; word-break: break-word;
  font-size: 13px; line-height: 1.45;
  transition: background .1s;
}
#table-body td:last-child { border-right: none; }
#table-body td.editable:not(.formula-col):not(.sep-col) { cursor: text; }
#table-body td.editable:not(.formula-col):not(.sep-col):hover { background: var(--accent-light); }
#table-body td.editing { padding: 0; }

/* Column widths */
.col-A, .col-B  { min-width: 120px; max-width: 150px; }
.col-C, .col-D, .col-E, .col-F, .col-G,
.col-H, .col-I, .col-J, .col-K, .col-L,
.col-M, .col-N, .col-O { min-width: 160px; max-width: 300px; }
.col-Q { min-width: 180px; max-width: 220px; }
.col-S { min-width: 110px; max-width: 140px; }

/* Separator columns */
.sep-col { min-width: 8px; max-width: 8px; width: 8px; background: var(--separator-bg); padding: 0 !important; }

/* Formula column (Q) */
.formula-col { color: var(--text-faint); font-style: italic; cursor: default !important; }
th.formula-col { font-style: italic; }

/* Inline edit textarea */
.cell-editor {
  width: 100%; min-height: 28px; padding: 6px 10px;
  border: 2px solid var(--accent); border-radius: 0;
  outline: none; resize: none; font-size: 13px; font-family: inherit;
  line-height: 1.45; background: var(--accent-light); color: var(--text);
  overflow: hidden;
}

/* Flash animations */
@keyframes flash-success { 0%,100% { background: inherit; } 30% { background: var(--success-light); } }
@keyframes flash-error   { 0%,100% { background: inherit; } 30% { background: var(--danger-light);  } }
.flash-success { animation: flash-success .7s ease; }
.flash-error   { animation: flash-error   .7s ease; }

/* Status badges */
.status-badge {
  display: inline-block; padding: 2px 8px; border-radius: 100px;
  font-size: 11px; font-weight: 600; letter-spacing: .3px; white-space: nowrap;
}
.status-Final      { background: #dcfce7; color: #166534; }
.status-In-progress { background: #fef9c3; color: #854d0e; }
.status-Temporal   { background: #e0e7ff; color: #3730a3; }
.status-Deprecated { background: #fee2e2; color: #991b1b; }
.status-default    { background: var(--border); color: var(--text-muted); }

/* ── Pagination ── */
#pagination {
  height: 44px; background: var(--surface); border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; gap: 16px; flex-shrink: 0;
}
#page-size-wrap { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-muted); }
#page-size-select {
  padding: 3px 6px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; color: var(--text); background: var(--bg); cursor: pointer;
}
#page-info { font-size: 13px; color: var(--text-muted); }
#page-nav { display: flex; align-items: center; gap: 8px; }
#page-current { font-size: 13px; color: var(--text-muted); white-space: nowrap; }
#page-prev, #page-next, #page-first, #page-last { padding: 4px 10px; font-size: 14px; }
#page-input {
  width: 48px; padding: 3px 6px; text-align: center;
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; color: var(--text); background: var(--bg); outline: none;
  transition: border-color .15s;
}
#page-input:focus { border-color: var(--accent); }
#page-input.invalid { border-color: var(--danger); background: var(--danger-light); color: var(--danger); }

/* ── Empty / Loading ── */
#empty-msg, #loading-msg {
  text-align: center; padding: 60px 20px; color: var(--text-muted); font-size: 15px;
}

/* ── Modal ── */
#modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
#modal {
  background: var(--surface); border-radius: 12px; box-shadow: var(--shadow-md);
  width: 620px; max-width: calc(100vw - 32px); max-height: calc(100vh - 48px);
  display: flex; flex-direction: column; overflow: hidden;
}
#modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 14px; border-bottom: 1px solid var(--border);
}
#modal-header h3 { font-size: 16px; font-weight: 600; }
#modal-body { padding: 16px 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
#modal-footer { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

#modal-count-row { display: flex; align-items: center; gap: 10px; }
#modal-count-row label { font-size: 13px; color: var(--text-muted); }
#modal-count { width: 64px; padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; text-align: center; }

#modal-rows-header {
  display: grid; grid-template-columns: 1fr 1fr 22px 22px;
  gap: 8px; padding: 0 2px;
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .4px; color: var(--text-muted);
}
.col-container { }
.col-key       { }

#modal-rows { display: flex; flex-direction: column; gap: 6px; }
.modal-row {
  display: grid; grid-template-columns: 1fr 1fr 22px 22px;
  gap: 8px; align-items: start;
}
.modal-row input {
  width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; color: var(--text); outline: none; transition: border-color .15s;
  background: var(--surface);
}
.modal-row input:focus { border-color: var(--accent); }
.modal-row input.error { border-color: var(--danger); background: var(--danger-light); }
.modal-row .row-status { display: flex; align-items: center; justify-content: center; font-size: 15px; padding-top: 7px; }
.modal-row .row-remove { display: flex; align-items: center; justify-content: center; padding-top: 4px; }
.row-remove-btn { background: none; border: none; cursor: pointer; color: var(--text-faint); font-size: 18px; line-height: 1; border-radius: 4px; padding: 2px 5px; }
.row-remove-btn:hover { color: var(--danger); background: var(--danger-light); }

/* ── Autocomplete dropdown ── */
#autocomplete-dropdown {
  position: fixed; z-index: 200;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; box-shadow: var(--shadow-md);
  max-height: 200px; overflow-y: auto; min-width: 160px;
}
.ac-item {
  padding: 7px 12px; cursor: pointer; font-size: 13px;
  transition: background .1s;
}
.ac-item:hover, .ac-item.active { background: var(--accent-light); color: var(--accent); }

/* ── Toast ── */
#toast {
  position: fixed; bottom: 24px; right: 24px; z-index: 300;
  background: #1e2130; color: #fff;
  padding: 10px 18px; border-radius: 8px; font-size: 13px;
  box-shadow: var(--shadow-md); max-width: 320px;
  animation: toast-in .2s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
#toast.toast-error { background: var(--danger); }

/* ── Cell selection ── */
td.selected {
  background-color: rgba(66, 133, 244, 0.15) !important;
  outline: 1px solid rgba(66, 133, 244, 0.5);
  outline-offset: -1px;
}

/* ── Row bar ── */
#table-body td.col-A,
#table-head th.col-A { padding-left: 22px; }

#row-bar {
  position: fixed; z-index: 50;
  width: 10px; height: 34px; border-radius: 5px;
  background: #c4c7cc;
  cursor: pointer;
  transform: translateY(-50%);
  transition: background .15s, width .15s;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
#row-bar.active { background: rgba(66,133,244,0.25); width: 18px; }
.row-bar-dots { display: none; flex-direction: column; align-items: center; gap: 3px; }
#row-bar.active .row-bar-dots { display: flex; }
.row-bar-dot { width: 3px; height: 3px; border-radius: 50%; background: rgba(66,133,244,0.9); }

/* ── Row selection ── */
tr.row-selected td { background: rgba(66,133,244,0.08) !important; }
tr.row-selected td.sep-col { background: rgba(66,133,244,0.05) !important; }
tr.row-danger td { background: rgba(239,68,68,0.08) !important; }
tr.row-danger td.sep-col { background: rgba(239,68,68,0.05) !important; }

/* ── Row context menu ── */
#row-context-menu {
  position: fixed; z-index: 500;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; box-shadow: var(--shadow-md);
  min-width: 160px; padding: 4px 0;
}
.ctx-item {
  padding: 8px 16px; cursor: pointer; font-size: 13px; color: var(--text);
  display: flex; align-items: center; gap: 8px;
}
.ctx-item svg { flex-shrink: 0; }
.ctx-item:hover { background: var(--accent-light); color: var(--accent); }
.ctx-item-danger { color: var(--danger); }
.ctx-item-danger:hover { background: var(--danger-light); color: var(--danger); }
.ctx-item-disabled { opacity: 0.35; cursor: default; }
.ctx-item-disabled:hover { background: transparent; color: var(--text); }

/* ── btn-danger ── */
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:not(:disabled):hover { background: #dc2626; }

/* ── Confirm dialog ── */
#confirm-overlay {
  position: fixed; inset: 0; z-index: 600;
  background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
}
#confirm-dialog {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow-md); padding: 24px; min-width: 320px; max-width: 480px;
}
#confirm-title { font-size: 15px; font-weight: 600; margin-bottom: 12px; }
#confirm-body { color: var(--text-muted); font-size: 13px; max-height: 240px; overflow-y: auto; margin-bottom: 20px; }
#confirm-body ul { padding-left: 16px; }
#confirm-body li { margin-bottom: 4px; }
#confirm-footer { display: flex; gap: 8px; justify-content: flex-end; }
