:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-alt: #fafbfc;
  --border: #e3e5e9;
  --border-strong: #d1d5db;
  --text: #16181d;
  --muted: #70757e;
  --accent: #2766c2;
  --accent-hover: #2159ab;
  --accent-contrast: #ffffff;
  --done-dim: 0.35;
  --canvas-bg: #eceae5;
  --grid-line: rgba(0,0,0,0.12);
  --cell-border: rgba(255,255,255,0.7);
  --shadow-sm: 0 1px 2px rgba(16,20,30,0.06), 0 1px 1px rgba(16,20,30,0.04);
  --shadow-md: 0 4px 12px rgba(16,20,30,0.10), 0 1px 3px rgba(16,20,30,0.08);
  --shadow-lg: 0 12px 32px rgba(16,20,30,0.18), 0 2px 8px rgba(16,20,30,0.10);
  --radius-sm: 7px;
  --radius-md: 10px;
  --radius-lg: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

html[data-theme="dark"] {
  --bg: #15161b;
  --panel: #1f2127;
  --panel-alt: #24262d;
  --border: #30333b;
  --border-strong: #3d414b;
  --text: #edeef1;
  --muted: #9298a3;
  --accent: #5b9bf5;
  --accent-hover: #74aafc;
  --accent-contrast: #0c1420;
  --canvas-bg: #15161a;
  --grid-line: rgba(0,0,0,0.25);
  --cell-border: rgba(255,255,255,0.4);
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 14px rgba(0,0,0,0.38);
  --shadow-lg: 0 14px 36px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ---------------- Custom tooltips ----------------
   Native `title` tooltips are unreliable (slow to appear, suppressed
   entirely in touch/WebView contexts) so every element that wants a
   tooltip exposes it as data-tooltip and a single shared floating
   element (#appTooltip, driven from app.js) is positioned next to
   whatever is currently hovered/focused. */

#appTooltip {
  position: fixed;
  z-index: 500;
  pointer-events: none;
  background: #1b1d22;
  color: #f3f4f6;
  font-size: 11.5px;
  font-weight: 600;
  padding: 5px 9px;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  opacity: 0;
  transform: translateY(2px);
  transition: opacity .1s ease, transform .1s ease;
  white-space: nowrap;
}

#appTooltip.visible {
  opacity: 1;
  transform: translateY(0);
}


/* ---------------- Landing page ---------------- */

.landing-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: var(--bg);
  overflow-y: auto;
  display: flex;
  justify-content: center;
}

.landing-overlay[hidden] { display: none; }

.landing-inner {
  width: 100%;
  max-width: 640px;
  padding: 48px 24px 64px;
}

.landing-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 28px;
}

.landing-header h1 {
  font-size: 24px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}

.landing-new-btn {
  width: 100%;
  justify-content: center;
  padding: 13px 16px;
  font-size: 14px;
  margin-bottom: 32px;
}

.landing-projects-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.landing-projects h2 {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  font-weight: 700;
  margin: 0;
}

.landing-filter-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}

.landing-filter-toggle input {
  cursor: pointer;
}

.landing-search-wrap {
  display: flex;
  align-items: stretch;
  gap: 8px;
  margin-bottom: 14px;
}

.landing-sort-select {
  flex-shrink: 0;
  width: 150px;
  height: 36px;
  background: var(--panel-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 28px 0 10px;
  font-size: 12.5px;
  font-family: inherit;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='12' height='12' fill='none' stroke='%2370757e' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
}

.landing-sort-select:focus {
  outline: none;
  border-color: var(--accent);
}

.landing-search-box {
  position: relative;
  flex: 1;
  min-width: 0;
}

.landing-search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}

.landing-search-input {
  width: 100%;
  height: 36px;
  box-sizing: border-box;
  background: var(--panel-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 10px 0 32px;
  font-size: 13px;
  font-family: inherit;
}

.landing-search-input:focus {
  outline: none;
  border-color: var(--accent);
}

/* Suppress the native search-input clear-button chrome in WebKit so it
   doesn't visually clash with the custom left-side search icon. */
.landing-search-input::-webkit-search-cancel-button {
  -webkit-appearance: none;
}

.landing-projects-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.landing-project-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  text-align: left;
  width: 100%;
  font-family: inherit;
  transition: background-color .12s ease, border-color .12s ease;
}

.landing-project-card:hover {
  background: var(--panel-alt);
  border-color: var(--border-strong);
}

.landing-project-thumb {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  overflow: hidden;
}

.landing-project-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.landing-project-meta {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.landing-project-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.landing-project-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.landing-project-byline {
  font-style: italic;
}

.landing-project-progress {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent);
  flex-shrink: 0;
}

.landing-project-delete {
  flex-shrink: 0;
  color: var(--muted);
}

.landing-project-delete:hover {
  color: #c53030;
}

.landing-project-download {
  flex-shrink: 0;
  color: var(--muted);
}

.landing-project-download:hover {
  color: var(--accent);
}

.landing-project-edit {
  flex-shrink: 0;
  color: var(--muted);
}

.landing-project-edit:hover {
  color: var(--accent);
}

.landing-empty {
  color: var(--muted);
  font-size: 13.5px;
  margin: 4px 2px;
}

/* ---------------- Top bar ---------------- */

#topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.topbar-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.topbar-actions {
  margin-left: auto;
  gap: 6px;
}

#topbar h1 {
  font-size: 15px;
  margin: 0;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.pattern-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  font-size: 12.5px;
  color: var(--muted);
  padding: 6px 0;
}

.pattern-meta-line {
  display: block;
}

.pattern-byline {
  font-style: italic;
}

#zoomInfo {
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.toolbar-divider {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 4px;
}

/* ---------------- Buttons ---------------- */

.btn {
  background: var(--panel-alt);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background-color .12s ease, border-color .12s ease, box-shadow .12s ease;
}

.btn:hover { background: var(--border); }

.btn.primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
  box-shadow: var(--shadow-sm);
}

.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.btn.secondary {
  background: transparent;
  border-color: var(--border-strong);
}

.btn.danger {
  background: #d64545;
  color: #ffffff;
  border-color: #d64545;
}

.btn.danger:hover:not(:disabled) { background: #c23a3a; border-color: #c23a3a; }

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-icon {
  font-size: 15px;
  line-height: 1;
}

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background-color .12s ease, color .12s ease;
}

.icon-btn:hover {
  background: var(--panel-alt);
  color: var(--text);
  border-color: var(--border);
}

.icon-btn.is-active {
  background: rgba(39,102,194,0.12);
  color: var(--accent);
  border-color: var(--accent);
}

html[data-theme="dark"] .icon-btn.is-active {
  background: rgba(91,155,245,0.16);
}

.icon-btn.is-active:hover {
  background: rgba(39,102,194,0.18);
}

.icon-btn svg { display: block; }

.icon-btn .icon-sun { display: none; }
.icon-btn.is-dark .icon-moon { display: none; }
.icon-btn.is-dark .icon-sun { display: block; }

.color-btn {
  position: relative;
  overflow: hidden;
}

#highlightColorInput {
  position: absolute;
  inset: -4px;
  width: calc(100% + 8px);
  height: calc(100% + 8px);
  padding: 0;
  border: none;
  cursor: pointer;
  background: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
}

#patternName { font-weight: 600; color: var(--text); }

/* ---------------- Load modal ---------------- */

.load-modal-content {
  width: 420px;
  max-width: 90vw;
  padding: 22px;
}

.load-modal-title {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 700;
}

.load-modal-options {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.load-option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  cursor: pointer;
  background: none;
  text-align: left;
  width: 100%;
  font-family: inherit;
  transition: background-color .12s ease, border-color .12s ease;
}

.load-option[hidden] { display: none; }

.load-option.loading {
  color: var(--muted);
  pointer-events: none;
}

.load-option:hover {
  background: var(--panel-alt);
  border-color: var(--border-strong);
}

.load-option-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: var(--panel-alt);
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.load-option-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.load-option-text strong {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text);
}

.load-option-text small {
  font-size: 11.5px;
  color: var(--muted);
}

.load-modal-sep {
  height: 1px;
  background: var(--border);
  margin: 18px 0 14px;
}

.load-sample-btn {
  width: 100%;
  justify-content: center;
}


/* ---------------- Canvas area ---------------- */

#main {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

#canvasWrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--canvas-bg);
}

#chart, #revealCanvas {
  display: block;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

#chart {
  cursor: grab;
  touch-action: none;
}

#chart.dragging { cursor: grabbing; }

#revealCanvas {
  pointer-events: none;
  z-index: 0;
}

#chart {
  z-index: 1;
}

#emptyState {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: var(--muted);
  pointer-events: none;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#emptyState .empty-icon {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  color: var(--border-strong);
}

#emptyState p {
  margin: 0;
  font-size: 14px;
  font-weight: 500;
}

#zoomControls {
  position: absolute;
  bottom: 18px;
  right: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 3;
}

.zoom-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 19px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-md);
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  transition: transform .08s ease, background-color .12s ease;
}

.zoom-btn:hover { background: var(--panel-alt); }
.zoom-btn:active { transform: scale(0.94); }

#minimap {
  position: absolute;
  top: 14px;
  left: 14px;
  width: 136px;
  height: 136px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  z-index: 3;
  cursor: pointer;
}

/* ---------------- Legend panel (floating window) ---------------- */

#legendPanel.floating-panel {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 272px;
  max-height: calc(100% - 32px - 160px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  z-index: 10;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#legendPanel[hidden] { display: none; }

.floating-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 8px 10px 14px;
  cursor: grab;
  user-select: none;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.floating-panel-header:active { cursor: grabbing; }

.legend-scroll-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

.floating-panel-body {
  padding: 12px 14px 14px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
  /* Native scrollbars are unreliable across browsers/OSes (hidden by
     default on macOS/iOS unless a user accessibility setting is
     changed, unsupported styling in some engines) -- hide the native
     one everywhere and draw our own reliable #legendScrollThumb
     instead, positioned/sized from JS based on real scroll state. */
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.floating-panel-body::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
}

.custom-scroll-thumb {
  position: absolute;
  right: 3px;
  top: 0;
  width: 6px;
  border-radius: 4px;
  background: var(--border-strong);
  pointer-events: none;
  transition: background-color .12s ease;
}

.custom-scroll-thumb[hidden] { display: none; }

#legendPanel h2 {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  font-weight: 700;
  margin: 0;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  margin-bottom: 2px;
  border: 1px solid transparent;
  transition: background-color .1s ease;
}

.legend-row:hover {
  background: var(--panel-alt);
}

.legend-row.active {
  background: rgba(39,102,194,0.10);
  border-color: var(--accent);
}

html[data-theme="dark"] .legend-row.active {
  background: rgba(91,155,245,0.14);
}

.legend-swatch {
  width: 20px;
  height: 20px;
  border-radius: 5px;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,0.15);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.25);
}

.legend-symbol {
  width: 22px;
  height: 22px;
  border-radius: 5px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text);
  background: var(--panel-alt);
  border: 1px solid var(--border);
}

.legend-meta {
  flex: 1;
  min-width: 0;
}

.legend-label {
  font-size: 12.5px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.legend-count {
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.legend-count.zero {
  color: #2f9e52;
  font-weight: 700;
}

html[data-theme="dark"] .legend-count.zero {
  color: #6bd98a;
}

#progressSummary {
  flex-shrink: 0;
  margin: 0;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.7;
}

#progressSummary:empty {
  display: none;
}

#progressSummary strong {
  color: var(--text);
}

/* ---------------- Modal ---------------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10,12,18,0.65);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-content {
  position: relative;
  max-width: 90vw;
  max-height: 90vh;
  background: var(--panel);
  border-radius: var(--radius-lg);
  padding: 12px;
  box-shadow: var(--shadow-lg);
}

.modal-content img {
  display: block;
  max-width: 100%;
  max-height: calc(90vh - 24px);
  border-radius: var(--radius-sm);
}

.modal-close {
  position: absolute;
  top: -14px;
  right: -14px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel);
  box-shadow: var(--shadow-md);
}

/* ---------------- Type-to-confirm modal ---------------- */

.confirm-modal-content {
  width: 380px;
  max-width: 90vw;
  padding: 22px;
}

.confirm-modal-title {
  margin: 0 0 10px 0;
  font-size: 16px;
  font-weight: 700;
}

.confirm-modal-body {
  margin: 0 0 16px 0;
  font-size: 13.5px;
  color: var(--muted);
  line-height: 1.5;
}

.confirm-modal-label {
  margin: 0 0 8px 0;
  font-size: 13px;
  color: var(--text);
}

.confirm-modal-input {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--panel-alt);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  margin-bottom: 18px;
}

.confirm-modal-input:focus {
  outline: none;
  border-color: var(--accent);
}

.confirm-modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.details-modal-content {
  width: 420px;
  max-width: 90vw;
  padding: 22px;
}

.details-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-bottom: 18px;
}

.details-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12.5px;
  color: var(--muted);
}

.details-field input {
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--panel-alt);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
}

.details-field input:focus {
  outline: none;
  border-color: var(--accent);
}

.details-field input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.import-loading-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 36px;
  color: var(--text);
  font-size: 14px;
}

.import-loading-spinner {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  animation: import-spin 0.8s linear infinite;
}

@keyframes import-spin {
  to { transform: rotate(360deg); }
}
