/*
  Counterlock web version: the look of the desktop app (Qt's Fusion style with
  Counterlock's own light and dark palettes).

  The colours are NOT written here: js/theme.js reads them from
  assets/theme.json (the same file the desktop uses) and sets the --variables
  below on <html>. The two fallbacks right here only colour the page for the
  moment before that file has loaded.

  Sizes follow the desktop: 9 pt control text, 14/12/14/8 px window margins,
  8 px between rows. Below 700 px wide the top row stacks; on touch screens
  controls grow to at least 44 px and text boxes use 16 px text (smaller makes
  iPhones zoom in when you tap them).
*/

:root {
  --window: #fafafa;
  --text: #1a1a1a;
  --frame: color-mix(in srgb, var(--window) 72%, #000);
  --frame-light: color-mix(in srgb, var(--window) 84%, #000);
  --button-top: color-mix(in srgb, var(--button) 60%, #fff);
  --focus: var(--highlight, #0078d4);
  --control-font: 9pt;
  --control-height: 24px;
  --ui-font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --window: #202124;
  --text: #e8eaed;
  --frame: color-mix(in srgb, var(--window) 70%, #fff);
  --frame-light: color-mix(in srgb, var(--window) 82%, #fff);
  --button-top: color-mix(in srgb, var(--button) 88%, #fff);
  color-scheme: dark;
}

@font-face { font-family: "Inter"; font-weight: 400; font-display: swap; src: url("assets/fonts/Inter-Regular.ttf") format("truetype"); }
@font-face { font-family: "Inter"; font-weight: 500; font-display: swap; src: url("assets/fonts/Inter-Medium.ttf") format("truetype"); }
@font-face { font-family: "Inter"; font-weight: 600; font-display: swap; src: url("assets/fonts/Inter-SemiBold.ttf") format("truetype"); }
@font-face { font-family: "Inter"; font-weight: 700; font-display: swap; src: url("assets/fonts/Inter-Bold.ttf") format("truetype"); }

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
}
body {
  background: var(--window);
  color: var(--text);
  font: var(--control-font) / 1.35 var(--ui-font);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* Like the desktop, labels, buttons and bars aren't text you can select or
     click a text cursor into. Only the matchup text (.doc), the editor's file
     name and form fields are. */
  user-select: none;
  -webkit-user-select: none;
}
input, textarea { user-select: text; -webkit-user-select: text; }

.muted { color: var(--muted); }
.bold { font-weight: bold; }
.semibold { font-weight: 600; }
.stretch { flex: 1 1 auto; }
.grow-1 { flex: 1 1 0; min-width: 0; }
.grow-2 { flex: 2 1 0; min-width: 0; }

/* ---- the website bar ----------------------------------------------------- */

.site-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 16px;
  padding-top: max(6px, env(safe-area-inset-top));
  background: #1b1029;
  color: #e9ddf8;
  font: 13px/1.2 var(--ui-font);
}
.site-bar a { color: inherit; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; }
.site-link { font-weight: 600; letter-spacing: 0.01em; }
.site-back { opacity: 0.75; }
.site-bar a:hover { opacity: 1; text-decoration: underline; }

/* ---- loading ------------------------------------------------------------- */

.loading {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--mid, #6b7280);
  font-size: 14px;
  padding: 16px;
  text-align: center;
}
.loading img { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.45; } }
@media (prefers-reduced-motion: reduce) { .loading img { animation: none; } }

/* ---- the "window" --------------------------------------------------------- */

.window {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
  padding: 12px 14px 8px;
  padding-left: max(14px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  min-height: 0;
}
#main-view {
  /* On desktop widths the page is one screen tall and only the text scrolls,
     like the desktop window. */
  height: calc(100dvh - 32px);
}

/* ---- controls (Fusion-like) ----------------------------------------------- */

button, select, input, textarea {
  font: inherit;
  color: var(--text);
}
button {
  min-height: var(--control-height);
  padding: 2px 10px;
  border: 1px solid var(--frame);
  border-radius: 3px;
  background: linear-gradient(var(--button-top), var(--button));
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
}
button:hover:not(:disabled) { border-color: color-mix(in srgb, var(--frame) 70%, var(--highlight)); }
button:active:not(:disabled) { background: var(--button); }
button:disabled { color: var(--disabled-text); cursor: default; }
button.default { border-color: color-mix(in srgb, var(--frame) 40%, var(--highlight)); }

input[type="text"], textarea, select {
  min-height: var(--control-height);
  border: 1px solid var(--frame);
  border-radius: 2px;
  background: var(--base);
  padding: 2px 5px;
}
select { padding-right: 20px; cursor: pointer; }
textarea { resize: vertical; line-height: 1.35; padding: 3px 5px; width: 100%; }
input::placeholder, textarea::placeholder { color: var(--placeholder); opacity: 1; }
input::selection, textarea::selection { background: var(--highlight); color: var(--highlighted-text); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 1px var(--focus); }
input[type="checkbox"], input[type="radio"] {
  accent-color: var(--highlight);
  width: 14px; height: 14px; margin: 0;
}

.panel {
  border: 1px solid var(--frame);
  border-radius: 2px;
}
.caption { display: block; white-space: nowrap; }

/* ---- top bar -------------------------------------------------------------- */

.top-bar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 10px;
}
.picker {
  display: grid;
  grid-template-columns: minmax(200px, 1fr) minmax(140px, auto);
  column-gap: 6px;
  row-gap: 3px;
  flex: 0 1 auto;
}
.display-controls { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.display-row { display: flex; align-items: center; gap: 6px; }
.size-button { width: 38px; padding: 2px 0; }
.font-size { width: 26px; text-align: center; }
.settings-button { margin-left: 6px; }

/* The hero search box (js/combobox.js). */
.combo { position: relative; display: flex; min-width: 0; }
.combo input {
  flex: 1 1 auto;
  min-width: 0;
  padding-right: 22px;
}
.combo-arrow {
  position: absolute;
  right: 1px; top: 1px; bottom: 1px;
  width: 20px;
  min-height: 0;
  padding: 0;
  border: 0;
  border-left: 1px solid var(--frame-light);
  border-radius: 0 2px 2px 0;
  background: transparent;
  color: var(--text);
  font-size: 9px;
}
.combo-list {
  position: absolute;
  z-index: 20;
  left: 0; right: 0; top: 100%;
  margin: 1px 0 0;
  padding: 1px 0;
  list-style: none;
  max-height: min(340px, 60vh);
  overflow-y: auto;
  background: var(--base);
  border: 1px solid var(--frame);
  box-shadow: 0 3px 10px rgb(0 0 0 / 0.18);
}
.combo-list li { padding: 2px 6px; cursor: pointer; white-space: nowrap; }
.combo-list li.active { background: var(--highlight); color: var(--highlighted-text); }
.combo-list li[aria-disabled="true"] { color: var(--disabled-text); cursor: default; }
.combo-list li[aria-disabled="true"].active { background: transparent; color: var(--disabled-text); }
.combo-list .empty { color: var(--placeholder); cursor: default; }

/* ---- selection row, content, bottom bar ------------------------------------ */

.selection-row { padding: 6px 10px; min-height: 30px; }

.content {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}
.doc {
  /* Margins (6, 4, 12, 4) and the 860 px cap come from core/render.py via
     js/main.js (--content-margins, --max-content-width). */
  padding: var(--content-margins, 4px 12px 4px 6px);
  max-width: calc(var(--max-content-width, 860px) + 18px);
  user-select: text;
  -webkit-user-select: text;
}

.bottom-bar { display: flex; justify-content: flex-end; }
.bottom-bar button {
  display: flex;
  align-items: center;
  gap: var(--bottom-bar-gap, 28px);
  padding: 4px 10px;
  min-height: 0;
  background: none;
  border-radius: 2px;
  font: inherit;
  text-align: left;
}
.bar-full { flex: 1 1 auto; min-width: 0; overflow: hidden; }
.bar-part { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-stretch { flex: 1 1 auto; margin-left: calc(-1 * var(--bottom-bar-gap, 28px)); }
.storage-note { margin: 0; text-align: right; font-size: 8pt; }

/* ---- dialogs ---------------------------------------------------------------- */

.dialog {
  padding: 0;
  border: 1px solid var(--frame);
  border-radius: 4px;
  background: var(--window);
  color: var(--text);
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.35);
  max-width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
}
.dialog::backdrop { background: rgb(0 0 0 / 0.3); }
.dialog-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 12px 14px 12px;
}
.dialog-title {
  margin: 0;
  font-size: 10pt;
  font-weight: 600;
}
.dialog-footer { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.settings-dialog { width: 430px; }
.profiles-dialog { width: 520px; }
.message-dialog { width: 420px; }
.msg-text { margin: 0; white-space: pre-line; }
.msg-detail { margin: 0; white-space: pre-line; color: var(--text); }
.msg-detail:empty, .msg-text:empty { display: none; }
#msg-input { width: 100%; }
.msg-buttons-right { justify-content: flex-end; }

/* A QGroupBox. */
.group {
  margin: 0;
  padding: 8px 10px 9px;
  border: 1px solid var(--frame);
  border-radius: 3px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}
.group legend { padding: 0 3px; margin-left: -3px; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.radio-row { display: flex; gap: 18px; }
.setting-row { display: flex; align-items: center; gap: 6px; }
.text-group { gap: 8px; }
.step-button { width: 32px; padding: 2px 0; }
.spacing-value { width: 52px; text-align: center; }
.button-row { display: flex; gap: 6px; }
.button-row button { flex: 1 1 0; }
.profile-label { margin: 0; }
.hint { margin: 0; }

.profile-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--frame);
  border-radius: 2px;
  background: var(--base);
  max-height: 46dvh;
  overflow-y: auto;
}
.profile-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--frame-light);
}
.profile-list li:last-child { border-bottom: 0; }
.profile-list .names { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.profile-list .file { overflow-wrap: anywhere; }
.profile-list .current .file { font-weight: 600; }
.profile-list .pname { color: var(--muted); font-size: 8pt; overflow-wrap: anywhere; }
.profile-list .empty { color: var(--muted); padding: 10px; }

.context-menu {
  position: fixed;
  z-index: 50;
  background: var(--base);
  border: 1px solid var(--frame);
  box-shadow: 0 3px 10px rgb(0 0 0 / 0.2);
  padding: 2px 0;
}
.context-menu button { border: 0; background: none; border-radius: 0; width: 100%; text-align: left; padding: 3px 18px; }
.context-menu button:hover { background: var(--highlight); color: var(--highlighted-text); }

/* ---- Profile editor ------------------------------------------------------------ */

.editor { max-width: 1200px; padding: 10px; gap: 8px; }
#editor-view { height: calc(100dvh - 32px); }
.editor-toolbar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.ed-status { margin-left: 8px; font-style: italic; }
.ed-file { user-select: text; overflow-wrap: anywhere; }
.meta-group { flex-direction: row; align-items: center; }
.editor-split { flex: 1 1 auto; min-height: 0; display: flex; gap: 8px; }
.enemy-panel { flex: 0 0 230px; display: flex; flex-direction: column; gap: 4px; min-height: 0; }
.enemy-list {
  flex: 1 1 auto;
  min-height: 120px;
  margin: 0;
  padding: 1px 0;
  list-style: none;
  overflow-y: auto;
  border: 1px solid var(--frame);
  border-radius: 2px;
  background: var(--base);
}
.enemy-list li { padding: 2px 6px; cursor: pointer; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.enemy-list li[aria-selected="true"] { background: var(--highlight); color: var(--highlighted-text); }
.enemy-buttons { display: flex; gap: 4px; }
.enemy-buttons button { flex: 1 1 auto; padding-left: 4px; padding-right: 4px; }
.enemy-form {
  flex: 1 1 auto;
  min-width: 0;
  overflow-y: auto;
  padding: 0 4px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.enemy-form[inert] { opacity: 0.55; }
.ident-row, .tips-hero-row, .build-row { display: flex; align-items: center; gap: 6px; }
.counters-group { gap: 8px; }
.build-row select { min-width: 170px; }
.build-hint { margin: 0; }
.tips-hero-row select { flex: 1 1 auto; min-width: 0; }
.titled { display: flex; flex-direction: column; gap: 3px; }
.rows { display: flex; flex-direction: column; gap: 4px; }
.row { display: flex; gap: 6px; align-items: center; }
.row .item-name { flex: 0 1 200px; min-width: 150px; }
.row .item-note, .row .tip { flex: 1 1 auto; min-width: 0; }
.row .remove { min-width: 24px; width: 24px; padding: 0; border-color: transparent; background: none; }
.row .remove:hover { border-color: var(--frame); background: var(--button); }
.rows > .add { align-self: stretch; }

/* ---- narrow screens (phones) ----------------------------------------------------- */

@media (max-width: 700px) {
  #main-view, #editor-view { height: auto; flex: 1 0 auto; }
  .top-bar { flex-direction: column; align-items: stretch; gap: 10px; }
  #your-picker { order: 2; }
  .display-controls { order: 3; align-items: stretch; }
  .display-controls .caption { text-align: left; }
  .display-row { justify-content: flex-start; flex-wrap: wrap; }
  .picker { grid-template-columns: minmax(0, 1fr) minmax(110px, 38%); }
  .content { overflow: visible; flex: 1 0 auto; }
  .doc { max-width: none; }
  .bar-full { flex-wrap: wrap; align-content: center; gap: 4px 16px; }
  .bar-stretch { display: none; }
  .editor-split { flex-direction: column; }
  .enemy-panel { flex: none; }
  .enemy-list { max-height: 32dvh; }
  .enemy-form { overflow: visible; }
  .meta-group { flex-wrap: wrap; }
  .meta-group input { min-width: 9em; }
  .ident-row, .build-row { flex-wrap: wrap; }
  .build-row select { flex: 1 1 100%; min-width: 0; }
  .row { flex-wrap: wrap; }
  .row .item-name { flex: 1 1 100%; min-width: 0; }
  .settings-dialog, .profiles-dialog, .message-dialog { width: calc(100vw - 16px); max-width: none; }
  .setting-row { flex-wrap: wrap; }
}

/* ---- touch screens ------------------------------------------------------------------ */

@media (pointer: coarse) {
  :root { --control-height: 44px; }
  input[type="text"], textarea, select { font-size: 16px; }
  .combo-list li, .enemy-list li { padding: 11px 10px; }
  .combo-arrow { width: 40px; font-size: 12px; }
  .combo input { padding-right: 42px; }
  .size-button, .step-button { width: 48px; }
  .check { min-height: 44px; }
  input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; }
  .bottom-bar button { min-height: 44px; }
  .row .remove { width: 44px; min-width: 44px; }
}
