/* WH40K 10e platform — minimal dark theme */

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", Roboto, sans-serif;
  background: #15171c;
  color: #e6e6e6;
  line-height: 1.5;
  font-size: 16px;
}

a { color: #ffb84d; text-decoration: none; }
a:hover { text-decoration: underline; }

header.top {
  background: #0c0e12;
  border-bottom: 2px solid #c33;
  padding: 0.8rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1.5rem;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 50;
}

/* Hamburger button — hidden on desktop, visible on small screens. */
.nav-toggle {
  display: none;
  background: transparent; border: 0; cursor: pointer;
  padding: 0.4rem 0.5rem; margin: -0.4rem 0;
  border-radius: 4px;
}
.nav-toggle:hover { background: #2a2d35; }
.nav-toggle span {
  display: block; width: 22px; height: 2px;
  background: #d0d0d0; margin: 4px 0; border-radius: 1px;
}

/* Close button — only visible inside the drawer (i.e. on small screens). */
.nav-close {
  display: none;
  position: absolute; top: 0.5rem; right: 0.5rem;
  background: transparent; border: 0; cursor: pointer;
  color: #ddd; font-size: 1.6rem; line-height: 1;
  padding: 0.2rem 0.6rem;
}

.nav-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 90;
}
header.top .brand {
  font-weight: bold;
  font-size: 1.05rem;
  color: #ffb84d;
}
header.top nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}
header.top nav a {
  color: #d0d0d0;
  padding: 0.35rem 0.75rem;
  border-radius: 3px;
  white-space: nowrap;
}
header.top nav a:hover { background: #2a2d35; text-decoration: none; }
header.top nav a.active { background: #c33; color: #fff; }
header.top .user-menu {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
header.top .user-menu a {
  color: #d0d0d0;
  padding: 0.35rem 0.6rem;
  border-radius: 3px;
}
header.top .user-menu a:hover { background: #2a2d35; text-decoration: none; }
header.top .user-menu a.active { background: #c33; color: #fff; }

nav.admin-nav {
  display: flex;
  gap: 0.25rem;
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid #2a2d35;
  flex-wrap: wrap;
}
nav.admin-nav a {
  color: #d0d0d0;
  padding: 0.35rem 0.75rem;
  border-radius: 3px;
  font-size: 0.9rem;
}
nav.admin-nav a:hover { background: #2a2d35; text-decoration: none; }
nav.admin-nav a.active { background: #5a4d1f; color: #fff2c0; }

.unit-tabs .tab-headers {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15rem;
  border-bottom: 1px solid #2a2d35;
  margin-bottom: 0.5rem;
}
.unit-tabs .tab-header {
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 0.4rem 0.7rem;
  color: #aaa;
  cursor: pointer;
  font-size: 0.9rem;
  white-space: nowrap;
}
.unit-tabs .tab-header:hover:not(:disabled) { color: #fff; background: #2a2d35; }
.unit-tabs .tab-header.active {
  color: #ffb84d;
  border-bottom-color: #c33;
  background: #1c1f26;
}
.unit-tabs .tab-header:disabled { color: #555; cursor: default; }
.unit-tabs .tab-header .muted { color: inherit; opacity: 0.6; font-size: 0.85em; }
.unit-tabs .tab-panel { display: none; }
.unit-tabs .tab-panel.active { display: block; }

ul.stat-strip {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 1.2rem;
  font-size: 0.78rem;
  color: #888;
}
ul.stat-strip li { display: inline-flex; align-items: baseline; gap: 0.3rem; }
ul.stat-strip .num { color: #ccc; font-weight: 600; font-size: 0.85rem; }
ul.stat-strip a { color: #aaa; font-size: 0.72rem; }
ul.stat-strip a:hover { color: #ffb84d; }

main {
  max-width: 1280px;
  margin: 1.2rem auto;
  padding: 0 1.5rem;
}

h1 { margin-top: 0; font-size: 1.5rem; border-bottom: 1px solid #2a2d35; padding-bottom: 0.5rem; }
h2 { font-size: 1.2rem; color: #ffb84d; }
h3 { font-size: 1rem; }

.flashes { list-style: none; padding: 0; margin: 0 0 1rem; }
.flashes li { padding: 0.5rem 0.8rem; border-radius: 4px; margin-bottom: 0.3rem; }
.flashes li.ok { background: #1f4d2c; }
.flashes li.error { background: #5a1f1f; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  margin: 1rem 0;
}
.card {
  background: #1c1f26;
  border: 1px solid #2a2d35;
  border-radius: 6px;
  padding: 1rem;
}
.card h3 { margin: 0 0 0.4rem; }
.card .stat { font-size: 1.6rem; color: #ffb84d; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.6rem 1rem;
  margin-bottom: 1rem;
}
.form-grid label { display: block; font-size: 0.85rem; color: #aaa; }
.form-grid input, .form-grid select, .form-grid textarea {
  width: 100%;
  padding: 0.5rem 0.6rem;
  background: #0c0e12;
  border: 1px solid #2a2d35;
  color: #e6e6e6;
  border-radius: 3px;
  font: inherit;
  font-size: 16px; /* prevent iOS zoom on focus */
  min-height: 40px;
}
.form-grid textarea { min-height: 4.5rem; }
input[type="number"] { width: 6rem; min-height: 40px; }
input, select, textarea, button { font-size: 16px; }
textarea { min-height: 4rem; resize: vertical; }
input[type="file"] { font-size: 0.9rem; padding: 0.5rem; }

button, .btn {
  display: inline-block;
  padding: 0.5rem 1rem;
  min-height: 40px;
  background: #c33;
  color: #fff;
  border: 0;
  border-radius: 3px;
  cursor: pointer;
  font: inherit;
  text-decoration: none;
  touch-action: manipulation;
}
button:hover, .btn:hover { background: #d44; text-decoration: none; }
.btn-ghost { background: transparent; border: 1px solid #444; color: #d0d0d0; }
.btn-ghost:hover { background: #2a2d35; }
.btn-small { padding: 0.25rem 0.6rem; font-size: 0.85rem; min-height: 32px; }

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0.5rem 0 1rem;
  border-radius: 4px;
}
table {
  width: 100%;
  border-collapse: collapse;
  background: #1c1f26;
}
.table-wrap > table { margin: 0; min-width: 100%; }
table th, table td {
  text-align: left;
  padding: 0.5rem 0.7rem;
  border-bottom: 1px solid #2a2d35;
  vertical-align: top;
}
table th { background: #0c0e12; color: #ffb84d; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.5px; }
table tr:hover td { background: #232730; }
table.compact th, table.compact td { padding: 0.3rem 0.5rem; font-size: 0.9rem; }

.stat-line { display: flex; gap: 0.6rem; flex-wrap: wrap; margin: 0.5rem 0; }
.stat-line .stat-cell {
  background: #0c0e12;
  border: 1px solid #2a2d35;
  border-radius: 3px;
  padding: 0.3rem 0.6rem;
  min-width: 3rem;
  text-align: center;
}
.stat-line .stat-cell .label { display: block; font-size: 0.7rem; color: #888; text-transform: uppercase; }
.stat-line .stat-cell .val   { display: block; font-weight: bold; color: #ffb84d; }

.kw-tag {
  display: inline-block;
  background: #2a2d35;
  border-radius: 3px;
  padding: 0.1rem 0.5rem;
  margin: 0.1rem 0.2rem 0.1rem 0;
  font-size: 0.8rem;
  color: #ddd;
}
.kw-tag.primary { background: #c33; color: #fff; }

.muted { color: #888; }
.right { text-align: right; }

.layout-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
}
@media (max-width: 880px) {
  .layout-split { grid-template-columns: 1fr; }
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.6rem;
  margin: 1rem 0;
}
.image-grid figure {
  margin: 0;
  background: #0c0e12;
  border: 1px solid #2a2d35;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
}
.image-grid img { display: block; width: 100%; height: 180px; object-fit: cover; }
.image-grid form {
  position: absolute; top: 4px; right: 4px;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 10px;
  font-size: 0.75rem;
  background: #2a2d35;
  color: #ddd;
}
.badge.status-done, .badge.status-based { background: #1f4d2c; color: #d0ffdc; }
.badge.status-wip { background: #5a4d1f; color: #fff2c0; }
.badge.status-unpainted { background: #4a4a4a; color: #ddd; }
.badge.status-primed { background: #1f3a5a; color: #cfe1ff; }

.section { margin: 1.2rem 0; }
.section h2 { border-bottom: 1px solid #2a2d35; padding-bottom: 0.3rem; }

dialog, .panel {
  background: #1c1f26;
  border: 1px solid #2a2d35;
  border-radius: 6px;
  padding: 1rem;
}

/* ============================================================
   Mobile responsiveness
   ============================================================ */

/* Tablet and below */
@media (max-width: 768px) {
  header.top {
    padding: 0.5rem 0.75rem;
    gap: 0.5rem 0.75rem;
  }
  header.top .brand { font-size: 1rem; flex: 1; }
  header.top nav a {
    padding: 0.7rem 0.8rem;
    font-size: 1rem;
  }

  /* Show hamburger, hide the inline nav. The drawer slides in when toggled. */
  .nav-toggle { display: block; order: -1; }
  .nav-close { display: block; }

  .primary-nav {
    position: fixed;
    top: 0; left: -50%;
    width: 39%; max-width: 160px;
    height: 100vh;
    background: #0c0e12;
    border-right: 2px solid #c33;
    z-index: 100;
    padding: 2.6rem 0.35rem 1rem;
    display: flex; flex-direction: column;
    gap: 0.15rem;
    overflow-y: auto;
    transition: left 0.2s ease-out;
    box-shadow: 4px 0 16px rgba(0,0,0,0.4);
  }
  .primary-nav a, .primary-nav .user-menu a {
    padding: 0.55rem 0.55rem;
    font-size: 0.92rem;
  }
  .primary-nav.open { left: 0; }
  .primary-nav a, .primary-nav .user-menu a {
    display: block; width: 100%; box-sizing: border-box;
    text-align: left; padding: 0.7rem 0.8rem;
    border-radius: 4px;
  }
  .primary-nav .user-menu {
    display: flex; flex-direction: column; align-items: stretch;
    gap: 0.3rem;
    margin: 0.6rem 0 0;
    padding-top: 0.6rem;
    border-top: 1px solid #2a2d35;
  }
  .primary-nav .user-menu form { display: flex; }
  .primary-nav .user-menu form button { width: 100%; padding: 0.7rem; }

  body.nav-open { overflow: hidden; }

  main {
    padding: 0 0.75rem;
    margin: 0.75rem auto;
  }

  h1 { font-size: 1.25rem; }
  h2 { font-size: 1.05rem; }

  .panel { padding: 0.75rem; }
  .card  { padding: 0.75rem; }
  .card .stat { font-size: 1.3rem; }

  .form-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  .cards {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.6rem;
  }

  table.compact th, table.compact td { padding: 0.35rem 0.45rem; font-size: 0.85rem; }
  table th, table td { padding: 0.45rem 0.55rem; }

  .image-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 0.5rem;
  }
  .image-grid img { height: 140px; }

  .stat-line { gap: 0.35rem; }
  .stat-line .stat-cell {
    min-width: 2.5rem;
    padding: 0.25rem 0.4rem;
  }
  .stat-line .stat-cell .label { font-size: 0.65rem; }

  .kw-tag { font-size: 0.75rem; padding: 0.1rem 0.4rem; }

  /* Long catalogue names + faction selector cells can wrap nicely */
  td.muted, .muted-cell { white-space: normal; word-break: break-word; }
}

/* Phone */
@media (max-width: 480px) {
  header.top {
    padding: 0.5rem 0.6rem;
    gap: 0.4rem;
  }
  header.top .brand { font-size: 0.95rem; }

  main { padding: 0 0.6rem; }

  h1 { font-size: 1.15rem; padding-bottom: 0.35rem; }

  .cards { grid-template-columns: 1fr 1fr; }
  .card { padding: 0.6rem; }
  .card .stat { font-size: 1.15rem; }

  .image-grid {
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 0.4rem;
  }
  .image-grid img { height: 110px; }
  .image-grid figcaption { padding: 0.3rem 0.4rem !important; font-size: 0.85rem; }

  .stat-line .stat-cell { min-width: 2.2rem; padding: 0.2rem 0.35rem; }
  .stat-line .stat-cell .val { font-size: 0.95rem; }

  button, .btn { padding: 0.5rem 0.8rem; }
  .btn-small { padding: 0.25rem 0.5rem; min-height: 30px; }

  /* Inline forms inside table cells (army add buttons) — let them wrap */
  td form { display: inline-flex; flex-wrap: wrap; gap: 0.25rem; align-items: center; }
  td input[type="number"] { width: 3rem !important; }
}

/* Very narrow */
@media (max-width: 360px) {
  .cards { grid-template-columns: 1fr; }
}

/* Cover-image pane on the list edit page. */
.cover-pane {
  display: flex; gap: 1rem; flex-wrap: wrap;
  background: #1c1f26;
  border: 1px solid #2a2d35;
  border-radius: 6px;
  padding: 1rem;
}
.cover-pane .cover-preview {
  flex: 0 0 200px;
  height: 200px;
  background: #0c0e12;
  border: 1px solid #2a2d35;
  border-radius: 4px;
  overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}
.cover-pane .cover-preview img {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.cover-pane .cover-preview .cover-empty {
  color: #555; font-style: italic; font-size: 0.9rem;
}
.cover-pane .cover-actions {
  flex: 1; min-width: 220px;
  display: flex; flex-direction: column; gap: 0.5rem;
}
@media (max-width: 480px) {
  .cover-pane .cover-preview { flex: 1 1 100%; height: 220px; }
}

/* ============================================================
   Image editor modal
   ============================================================ */

body.ie-modal-open { overflow: hidden; }

#ie-modal {
  position: fixed; inset: 0;
  background: rgba(8, 9, 12, 0.92);
  z-index: 1000;
  display: flex; flex-direction: column;
  /* Touch interactions inside the editor must not scroll the page. */
}

.ie-instructions {
  background: #5a4d1f;
  color: #fff2c0;
  text-align: center;
  font-weight: 600;
  font-size: 0.95rem;
  padding: 0.55rem 0.8rem;
  border-bottom: 1px solid #2a2d35;
}

.ie-toolbar {
  display: flex; align-items: center; flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.6rem 0.8rem;
  background: #0c0e12;
  border-bottom: 1px solid #2a2d35;
}
.ie-toolbar .ie-spacer { flex: 1; }

.ie-btn, .ie-btn-ghost, .ie-btn-primary {
  font: inherit; cursor: pointer;
  min-height: 40px; padding: 0.45rem 0.8rem;
  border-radius: 4px; border: 1px solid #2a2d35;
  background: #1c1f26; color: #ddd;
}
.ie-btn:hover, .ie-btn-ghost:hover { background: #2a2d35; }
.ie-btn-primary {
  background: #c33; border-color: #c33; color: #fff;
}
.ie-btn-primary:hover { background: #e44; }
.ie-btn-ghost { background: transparent; }

.ie-stage {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding: 0.5rem;
  overflow: hidden;
  flex-direction: column;
  gap: 0.8rem;
}
.ie-empty {
  color: #888; font-size: 1rem; text-align: center; max-width: 360px;
  padding: 1rem; line-height: 1.5;
}
.ie-canvas-wrap {
  position: relative;
  max-width: 100%; max-height: 100%;
  /* Stop touch from panning the page when dragging on the image. */
  touch-action: none;
}
.ie-canvas {
  display: block;
  max-width: 100%; max-height: calc(100vh - 120px);
  background: #000;
  cursor: crosshair;
  -webkit-user-select: none; user-select: none;
}
.ie-sel {
  position: absolute;
  border: 2px solid #ffb84d;
  background: rgba(255, 184, 77, 0.15);
  box-sizing: border-box;
  pointer-events: none;
}
.ie-handle {
  position: absolute;
  width: 24px; height: 24px;
  background: #ffb84d; border: 2px solid #fff;
  border-radius: 50%;
  pointer-events: auto;
  touch-action: none;
}
.ie-handle[data-h="nw"] { left: -12px; top: -12px;    cursor: nwse-resize; }
.ie-handle[data-h="ne"] { right: -12px; top: -12px;   cursor: nesw-resize; }
.ie-handle[data-h="sw"] { left: -12px; bottom: -12px; cursor: nesw-resize; }
.ie-handle[data-h="se"] { right: -12px; bottom: -12px; cursor: nwse-resize; }

@media (max-width: 480px) {
  .ie-toolbar { padding: 0.45rem; gap: 0.3rem; }
  .ie-instructions { font-size: 0.85rem; padding: 0.45rem 0.6rem; }
  .ie-btn, .ie-btn-ghost, .ie-btn-primary {
    padding: 0.45rem 0.55rem;
    font-size: 0.85rem;
  }
  /* Bigger touch targets for the handle dots on phones. */
  .ie-handle { width: 30px; height: 30px; }
  .ie-handle[data-h="nw"] { left: -15px; top: -15px; }
  .ie-handle[data-h="ne"] { right: -15px; top: -15px; }
  .ie-handle[data-h="sw"] { left: -15px; bottom: -15px; }
  .ie-handle[data-h="se"] { right: -15px; bottom: -15px; }
}

/* ============================================================
   Generic mobile fixes (additions to the existing media queries)
   ============================================================ */

.table-wrap { overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
.unit-tabs .tab-headers { overflow-x: auto; -webkit-overflow-scrolling: touch; }

@media (max-width: 480px) {
  /* All buttons big enough to tap reliably. */
  .btn, button.btn, button[type="submit"] {
    min-height: 40px;
  }
  /* Forms with .row layout should wrap. */
  form .row { flex-wrap: wrap; }
  /* Stat strip stays compact even on tiny phones. */
  ul.stat-strip { font-size: 0.72rem; gap: 0.3rem 0.7rem; }
  ul.stat-strip .num { font-size: 0.78rem; }
}
