:root {
  color-scheme: light;
  font-family: Inter, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
  color: #1d2a31;
  background: #eef2f3;
  font-synthesis: none;
  --line: #d8e0e2;
  --muted: #66777f;
  --surface: #ffffff;
  --soft: #f4f7f7;
  --green: #1e7a55;
  --green-soft: #e7f3ed;
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; min-height: 100vh; background: #eef2f3; }
button, input { font: inherit; }
button, label { -webkit-tap-highlight-color: transparent; }

.app-header {
  min-height: 68px;
  padding: 10px 18px;
  display: flex;
  align-items: center;
  gap: 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand-block { display: flex; align-items: center; gap: 11px; min-width: 230px; }
.brand-logo-wrap {
  width: 44px; height: 34px; flex: 0 0 44px; overflow: hidden;
}
/* icon.png combines a square mark with a wordmark below it.  Keep the mark
   at its natural aspect ratio and clip only the lower wordmark. */
.brand-logo { width: 44px; height: auto; display: block; }
.brand-block h1 { margin: 0; font-size: 17px; line-height: 1.2; letter-spacing: 0; }
.status-line { margin-top: 3px; display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.status-line i { width: 7px; height: 7px; border-radius: 50%; background: #d19b3d; }
.status-line i.is-online { background: #2a9a68; }
.brand-text { min-width: 0; }
/* Help dialog. Opens once per browser (see phenome.helpSeen) and from the "?"
   button after that; content is fetched from /api/help so it follows the
   language toggle and matches the desktop guide. */
.help-overlay {
  position: fixed; inset: 0; z-index: 40; display: flex; align-items: center;
  justify-content: center; padding: 16px; background: rgba(20, 32, 38, .55);
}
.help-overlay[hidden] { display: none; }
.help-panel {
  width: min(680px, 100%); max-height: min(86vh, 900px); display: flex;
  flex-direction: column; background: var(--surface); border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .28); overflow: hidden;
}
.help-head {
  padding: 14px 18px; display: flex; align-items: center; justify-content: space-between;
  gap: 12px; border-bottom: 1px solid var(--line);
}
.help-head h2 { margin: 0; font-size: 16px; }
.help-body { padding: 4px 18px 18px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.help-body p.help-intro { margin: 14px 0 4px; color: #41525a; font-size: 14px; line-height: 1.7; }
.help-body h3 {
  margin: 20px 0 8px; font-size: 13px; color: var(--green);
  letter-spacing: .02em; text-transform: none;
}
.help-body ul { margin: 0; padding-left: 20px; }
.help-body li { margin: 0 0 8px; font-size: 13px; line-height: 1.7; color: #33474f; }
.help-button { min-width: 40px; font-weight: 700; }

.help-button { margin-left: auto; }
.language-toggle {
  min-width: 52px; min-height: 34px; padding: 5px 10px;
  border: 1px solid #cbd5d8; border-radius: 6px; background: #fff;
  color: #33474f; font-size: 13px; cursor: pointer;
}

/* Toolbar hierarchy: opening an image is the lead action, analysis is the
   working set, calibration is a once-per-image detour that folds away. */
.toolbar {
  padding: 8px 12px; display: flex; align-items: flex-end; flex-wrap: wrap;
  gap: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.tool-group { min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.tool-group-primary { flex-direction: row; align-items: center; gap: 8px; }
.tool-group-end { margin-left: auto; }
.tool-group-label {
  color: var(--muted); font-size: 11px; letter-spacing: .04em; text-transform: uppercase;
}
.tool-row { display: flex; flex-wrap: wrap; gap: 6px; }
.tool-group-collapsible > summary { cursor: pointer; list-style: none; }
.tool-group-collapsible > summary::-webkit-details-marker { display: none; }
.tool-group-collapsible > summary::before { content: "\25b8 "; }
.tool-group-collapsible[open] > summary::before { content: "\25be "; }
.tool-group-collapsible[open] { gap: 5px; }

.command {
  min-height: 36px; padding: 7px 11px; border: 1px solid #cbd5d8; border-radius: 6px;
  background: #fff; color: #263840; cursor: pointer; white-space: nowrap; font-size: 14px;
}
/* Three visual weights, matching the three tiers. */
/* Two class names, one element: the pick button carries command-action and gains
   command-primary while it can save, so this needs to outrank the tier styles. */
.command.command-primary { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }
.command-action { background: var(--green-soft); border-color: #b9dccb; color: #14543a; }
.command-quiet { background: #fff; color: #4a5b62; font-size: 13px; }
.command-lead { min-height: 42px; padding: 9px 16px; display: inline-flex; align-items: center; gap: 8px; font-size: 15px; }
.command-icon { font-size: 18px; line-height: 1; }
.command.is-active { background: var(--green); border-color: var(--green); color: #fff; font-weight: 700; }
button:disabled { cursor: not-allowed; color: #9aa6aa; background: #f1f4f4; border-color: #e0e5e6; }
label.command { display: inline-flex; align-items: center; }

/* The toolbar wraps, so its height is not a constant any more: let the grid take
   the remaining space instead of subtracting a magic number. */
.unified-app { display: flex; flex-direction: column; min-height: calc(100vh - 68px); }

/* Progress strip: shared by the model load, the upload and every detection. */
.progress {
  display: flex; align-items: center; gap: 10px; padding: 7px 14px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.progress[hidden] { display: none; }
.progress-track {
  flex: 1; height: 6px; border-radius: 3px; background: #e3eaeb; overflow: hidden;
}
.progress-track > i {
  display: block; width: 0; height: 100%; border-radius: 3px;
  background: linear-gradient(90deg, #2a9a68, var(--green));
  transition: width 260ms ease;
}
/* Faint moving stripes so an estimated bar still reads as "working". */
.progress.is-running .progress-track > i {
  background-image: linear-gradient(90deg, #2a9a68, var(--green)),
    repeating-linear-gradient(115deg, rgba(255, 255, 255, .22) 0 8px, rgba(255, 255, 255, 0) 8px 18px);
  background-blend-mode: overlay;
  animation: progress-stripes 900ms linear infinite;
}
@keyframes progress-stripes { from { background-position: 0 0, 0 0; } to { background-position: 0 0, 36px 0; } }
#progress-label { color: #33474f; font-size: 12px; white-space: nowrap; }
#progress-percent { min-width: 3.2em; color: var(--muted); font-size: 12px; text-align: right; }
.workspace-grid {
  flex: 1; min-height: 480px; display: grid;
  grid-template-columns: minmax(180px, 220px) minmax(420px, 1fr) minmax(240px, 320px);
}
.panel-band { min-width: 0; background: var(--surface); }
.files-panel { border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.details-panel { border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.panel-heading {
  height: 48px; padding: 0 14px; display: flex; align-items: center;
  justify-content: space-between; border-bottom: 1px solid var(--line);
}
.panel-heading h2 { margin: 0; font-size: 14px; }
.panel-heading > span { color: var(--muted); font-size: 12px; }
.file-list { padding: 8px; flex: 1; overflow: auto; }
.panel-note { padding: 9px 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }
.empty-copy, .object-empty { color: #879399; font-size: 13px; text-align: center; }
.empty-copy { margin: 28px 0; }
.file-item {
  padding: 10px; overflow: hidden; border-left: 3px solid var(--green);
  background: var(--green-soft); font-size: 13px; text-overflow: ellipsis; white-space: nowrap;
}
.canvas-panel { min-width: 0; display: grid; grid-template-rows: minmax(0, 1fr) 34px; background: #cfd7d9; }
.image-stage { position: relative; min-height: 0; display: grid; place-items: center; overflow: hidden; }
.canvas-frame { position: relative; display: none; max-width: 100%; max-height: 100%; line-height: 0; }
.image-stage.has-image .canvas-frame { display: inline-block; }
.canvas-frame.is-picking { cursor: crosshair; }
.image-stage img { display: block; width: 100%; height: 100%; object-fit: contain; }
.image-stage.has-image .stage-empty { display: none; }
/* The overlay is a canvas, not an SVG: a few hundred SVG nodes with
   translucent fills are what made weak browsers stutter, and SVG user-unit
   line widths disappeared once the image was scaled down. Colours and stroke
   widths now live in app.js (see drawOverlay); only the box belongs here. */
.canvas-overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
/* The empty state is a <label> for the file input, so it must look and behave
   like a target: tapping the middle of the canvas is what people try first. */
.stage-empty {
  display: grid; justify-items: center; gap: 9px; padding: 26px 34px;
  border: 1px dashed #9facb0; border-radius: 12px; color: #64737a; cursor: pointer;
  background: rgba(255, 255, 255, .45); text-align: center;
}
.stage-empty:hover { border-color: var(--green); color: #2c4a3e; background: rgba(255, 255, 255, .75); }
.stage-hint { font-size: 12px; color: #7c8a90; }

/* Sample thumbnails: a picture is a better label than a name here. */
.sample-button {
  display: flex; align-items: center; gap: 8px; padding: 4px 10px 4px 4px;
  border: 1px solid #cbd5d8; border-radius: 8px; background: #fff;
  color: #33474f; font-size: 13px; cursor: pointer;
}
.sample-button img { width: 46px; height: 32px; object-fit: cover; border-radius: 5px; display: block; }
.sample-button:hover { border-color: var(--green); color: #14543a; }
.stage-symbol {
  width: 42px; height: 42px; display: grid; place-items: center;
  border: 1px solid #9facb0; border-radius: 50%; font-size: 26px;
}
.canvas-footer {
  padding: 0 12px; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: #fff; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px;
}
.canvas-footer span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.detail-tabs { justify-content: flex-start; gap: 16px; }
.detail-tab { height: 48px; padding: 0 2px; border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--muted); cursor: pointer; }
.detail-tab.is-active { border-bottom-color: var(--green); color: #243b34; font-weight: 700; }
.detail-body { flex: 1; min-height: 0; overflow: auto; }
.detail-pane { display: none; height: 100%; }
.detail-pane.is-active { display: block; }
.object-empty { padding: 28px 12px; }
.object-list { padding: 6px; display: grid; gap: 5px; }
.object-row {
  display: grid; grid-template-columns: minmax(0, 1fr) auto auto auto; align-items: center; gap: 6px;
  padding: 6px 7px; border: 1px solid var(--line); border-radius: 6px; background: #fff;
}
.object-row.is-selected { border-color: var(--green); background: var(--green-soft); }
.object-name {
  min-width: 0; padding: 2px 0; border: 0; background: transparent; color: #1d2a31;
  font-size: 13px; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer;
}
.object-area { color: var(--muted); font-size: 11px; }
.object-action {
  min-height: 28px; padding: 3px 7px; border: 1px solid #cbd5d8; border-radius: 5px;
  background: #fff; color: #33474f; font-size: 12px; cursor: pointer;
}
.object-action.is-danger { border-color: #e2b4b4; color: #9d3030; }
.trait-wrap { padding: 6px; overflow: auto; }
.trait-table { border-collapse: collapse; font-size: 12px; white-space: nowrap; }
.trait-table th, .trait-table td { padding: 5px 8px; border: 1px solid var(--line); text-align: right; }
.trait-table th { position: sticky; top: 0; background: var(--soft); text-align: center; }
.trait-table td:first-child, .trait-table th:first-child { text-align: left; }
.details-actions { padding: 10px; display: grid; grid-template-columns: 1fr 1fr; gap: 7px; border-top: 1px solid var(--line); }

.toast {
  position: fixed; left: 50%; bottom: calc(22px + env(safe-area-inset-bottom)); z-index: 10;
  max-width: min(90vw, 420px); padding: 10px 14px; transform: translate(-50%, 16px);
  border-radius: 6px; background: #17262b; color: #fff; opacity: 0; pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease; font-size: 13px;
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* Tablet: the object and trait panel moves below the canvas instead of hiding,
   because segmentation is unusable without the object list. */
@media (max-width: 900px) {
  .workspace-grid {
    height: auto; min-height: calc(100vh - 123px);
    grid-template-columns: 170px minmax(0, 1fr);
    grid-template-rows: minmax(52vh, 1fr) auto;
  }
  .files-panel, .canvas-panel { grid-row: 1; }
  .details-panel {
    grid-column: 1 / -1; grid-row: 2; max-height: 46vh;
    border-left: 0; border-top: 1px solid var(--line);
  }
}

@media (max-width: 640px) {
  .app-header { position: sticky; top: 0; z-index: 5; min-height: 62px; padding: 8px 12px; gap: 8px; }
  .brand-block { min-width: 0; flex: 1; }
  .brand-logo-wrap { width: 38px; height: 30px; flex-basis: 38px; }
  .brand-logo { width: 38px; height: auto; }
  .brand-block h1 { font-size: 15px; }
  .workspace-grid { grid-template-columns: 1fr; grid-template-rows: minmax(46vh, 1fr) auto; }
  .files-panel { display: none; }
  .details-panel { grid-column: 1; max-height: 52vh; }
  .canvas-panel { min-height: 46vh; }
  .toolbar { padding: 8px 10px; gap: 8px 10px; }
  .tool-group-end { margin-left: 0; }
  .command-lead { flex: 1; justify-content: center; }
  .tool-group-primary { width: 100%; }
  .canvas-footer { font-size: 11px; }
  .details-actions { grid-template-columns: 1fr 1fr; }
  .language-toggle { min-width: 46px; }
  .help-panel { max-height: 92vh; }
  .help-body li { font-size: 12.5px; }
}

@media (prefers-reduced-motion: reduce) { .toast { transition: none; } }
