:root {
  color-scheme: light;
  --bg: #f5f6f2;
  --surface: #ffffff;
  --ink: #202124;
  --muted: #68706a;
  --line: #dfe3dc;
  --brand: #1d6b5f;
  --brand-dark: #12483f;
  --good: #1f7a4d;
  --wait: #9b5c00;
  --bad: #b42318;
  --soft-good: #e8f6ee;
  --soft-wait: #fff3d8;
  --soft-bad: #fde8e6;
  --soft-muted: #edf0ec;
  --shadow: 0 8px 24px rgb(32 33 36 / 8%);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", "Noto Sans Thai", Tahoma, sans-serif;
  font-size: 16px;
}

a {
  color: inherit;
  text-decoration: none;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
.as-link {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
}

button:disabled {
  cursor: not-allowed;
  opacity: .45;
}

.primary {
  background: var(--brand);
  color: #fff;
  font-weight: 700;
}

.primary:hover {
  background: var(--brand-dark);
}

.ghost {
  background: #eef1ec;
  color: var(--ink);
  border: 1px solid var(--line);
  font-weight: 650;
}

.danger {
  background: var(--soft-bad);
  color: var(--bad);
  border-color: #f5c5bf;
}

.small {
  min-height: 38px;
  padding: 0 12px;
}

.live-indicator {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  border: 1px solid #c7ddd4;
  border-radius: 999px;
  padding: 0 11px;
  background: #eaf6f0;
  color: var(--brand-dark);
  font-size: 13px;
  font-weight: 800;
}

.data-source {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 11px;
  background: #fff;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.live-indicator::before {
  content: "";
  width: 8px;
  height: 8px;
  margin-right: 7px;
  border-radius: 999px;
  background: var(--good);
}

.live-indicator[data-tone="warn"] {
  border-color: #efd8a8;
  background: var(--soft-wait);
  color: var(--wait);
}

.live-indicator[data-tone="warn"]::before {
  background: var(--wait);
}

.full {
  width: 100%;
}

.app-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: rgb(255 255 255 / 92%);
  backdrop-filter: blur(12px);
}

.app-logo,
.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: var(--brand);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 800;
}

.top-nav {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}

.top-nav a {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  border-radius: 8px;
  color: var(--muted);
  white-space: nowrap;
}

.top-nav a.active {
  background: #e3eee9;
  color: var(--brand-dark);
  font-weight: 750;
}

.top-nav b {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
  background: var(--bad);
  color: #fff;
  font-size: 12px;
}

.icon-btn {
  width: 42px;
  padding: 0;
  background: #f1f3ef;
  color: var(--ink);
}

.page,
.chat-page {
  width: min(1040px, 100%);
  margin: 0 auto;
  padding: 18px 12px 40px;
}

.page-head {
  display: flex;
  gap: 16px;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.head-actions {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.page-head h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.1;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 750;
  text-transform: uppercase;
}

.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.auth-panel {
  width: min(420px, 100%);
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.brand-lockup {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 22px;
}

.brand-lockup h1 {
  margin: 0;
  font-size: 24px;
}

.brand-lockup p,
.helper,
.muted-line {
  color: var(--muted);
}

.stack,
.grid-form,
.action-form {
  display: grid;
  gap: 12px;
}

label span {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 10px 12px;
}

input[aria-invalid="true"],
select[aria-invalid="true"],
textarea[aria-invalid="true"] {
  border-color: var(--bad);
  box-shadow: 0 0 0 3px rgb(180 35 24 / 12%);
}

textarea {
  resize: vertical;
}

.stats-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.stat {
  min-height: 76px;
  padding: 12px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
}

.stat span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.stat strong {
  display: block;
  margin-top: 6px;
  font-size: 28px;
  line-height: 1;
}

.stat.good {
  background: var(--soft-good);
}

.stat.wait {
  background: var(--soft-wait);
}

.stat.bad {
  background: var(--soft-bad);
}

.segmented {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  overflow-x: auto;
  scrollbar-width: none;
}

.search-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 8px;
  margin-bottom: 14px;
}

.segmented a {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  white-space: nowrap;
}

.segmented a.active {
  border-color: var(--brand);
  color: var(--brand-dark);
  font-weight: 750;
}

.segmented b,
.count-pill {
  min-width: 24px;
  min-height: 24px;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
  padding: 0 7px;
  background: #e5e9e3;
  color: var(--ink);
  font-size: 13px;
}

.count-pop {
  animation: countPop .45s ease-out;
}

.job-list {
  display: grid;
  gap: 10px;
}

.request-list {
  display: grid;
  gap: 10px;
}

.request-group-stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin: 12px 0;
}

.request-stat {
  min-height: 66px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafbf8;
}

.request-stat span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.request-stat strong {
  display: block;
  margin-top: 4px;
  font-size: 22px;
}

.request-group-section,
.request-quick-group {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}

.request-group-section h3,
.request-quick-group h3 {
  margin: 0;
  font-size: 15px;
}

.quick-list {
  display: grid;
  gap: 6px;
}

.quick-list a {
  display: grid;
  gap: 2px;
  padding: 10px;
  border: 1px solid #efd8a8;
  border-radius: 8px;
  background: var(--soft-wait);
}

.quick-list span {
  font-weight: 750;
}

.quick-list small {
  color: var(--wait);
}

.template-source {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.section-count {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.image-output-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
}

.job-card,
.panel,
.request-card,
.image-output-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 14px;
  box-shadow: 0 2px 10px rgb(32 33 36 / 4%);
}

.job-card.live-changed {
  animation: liveFlash 1.2s ease-out;
}

.image-output-card {
  padding: 10px;
  box-shadow: none;
}

.image-thumb {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}

.image-thumb-button {
  min-height: 0;
  padding: 0;
  cursor: zoom-in;
}

.image-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.image-output-body {
  padding-top: 10px;
}

.image-output-body h3 {
  margin: 0;
  font-size: 16px;
  line-height: 1.25;
}

.job-main {
  display: flex;
  gap: 12px;
  justify-content: space-between;
  align-items: flex-start;
}

.job-main h2,
.panel h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.25;
}

.job-title-block {
  min-width: 0;
}

.buyer-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.buyer-link {
  min-width: 0;
}

.buyer-link:hover h2 {
  color: var(--brand);
}

.copy-buyer-button {
  min-height: 26px;
  padding: 0 8px;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1;
}

.job-order {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 760;
}

.job-meta,
.mini-image-row,
.button-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 12px;
}

.mock-form {
  margin-top: 12px;
}

.inline-form {
  display: inline-flex;
  margin: 0;
}

.compact-row {
  margin-top: 10px;
}

.job-meta span {
  min-height: 28px;
  display: inline-flex;
  align-items: center;
  padding: 0 8px;
  border-radius: 7px;
  background: #f0f2ee;
  color: var(--muted);
  font-size: 13px;
}

.job-time {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 12px;
}

.job-time span {
  white-space: nowrap;
}

.badge {
  min-height: 30px;
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.badge.good {
  background: var(--soft-good);
  color: var(--good);
}

.badge.wait {
  background: var(--soft-wait);
  color: var(--wait);
}

.badge.bad {
  background: var(--soft-bad);
  color: var(--bad);
}

.badge.muted {
  background: var(--soft-muted);
  color: var(--muted);
}

.mini-image-button {
  width: 56px;
  height: 56px;
  min-height: 56px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  cursor: zoom-in;
}

.mini-image-button img {
  display: block;
  width: 56px;
  height: 56px;
  object-fit: cover;
}

.detail-grid {
  display: grid;
  gap: 12px;
}

.facts {
  display: grid;
  gap: 10px;
  margin: 0;
}

.facts div {
  display: grid;
  grid-template-columns: minmax(110px, 34%) 1fr;
  gap: 12px;
}

.facts dt {
  color: var(--muted);
  font-weight: 750;
}

.facts dd {
  margin: 0;
}

.item-stack,
.log-list,
.event-list {
  display: grid;
  gap: 8px;
}

.item-line {
  display: grid;
  gap: 4px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafbf8;
}

.item-line small {
  color: var(--muted);
}

.text-block {
  white-space: pre-wrap;
  line-height: 1.55;
}

.brief-preview-panel {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fafbf8;
}

.saved-brief {
  margin-top: 12px;
}

.payload-details {
  margin-top: 10px;
  padding: 10px 0 0;
  border-top: 1px solid var(--line);
}

.payload-details summary {
  cursor: pointer;
  color: var(--brand-dark);
  font-weight: 750;
}

.payload-details .brief-head {
  margin-top: 10px;
}

.quality-line,
.validation-summary {
  margin: 10px 0 0;
  padding: 10px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
}

.quality-line.ready {
  background: var(--soft-good);
  color: var(--good);
}

.quality-line.check,
.validation-summary {
  background: var(--soft-wait);
  color: var(--wait);
}

.brief-head {
  display: flex;
  gap: 8px;
  justify-content: space-between;
  align-items: center;
}

.brief-head strong {
  font-size: 14px;
}

.brief-preview {
  margin: 0;
  max-height: 260px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--ink);
  font: 13px/1.5 "Cascadia Mono", Consolas, monospace;
}

.brief-preview.tall {
  max-height: 520px;
}

.draft-figure {
  margin: 0;
}

.image-button {
  width: 100%;
  height: auto;
  padding: 0;
  background: transparent;
  cursor: zoom-in;
}

.draft-figure img {
  width: 100%;
  max-height: 58vh;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.draft-figure figcaption {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.lightbox-open {
  overflow: hidden;
}

.lightbox-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgb(0 0 0 / 74%);
}

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

.lightbox-shell {
  width: min(1100px, 100%);
  max-height: 94vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
}

.lightbox-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.lightbox-shell img {
  max-width: 100%;
  max-height: calc(94vh - 58px);
  object-fit: contain;
  justify-self: center;
  align-self: center;
  border-radius: 8px;
  background: #fff;
}

.log-list code {
  display: block;
  padding: 10px;
  border-radius: 8px;
  background: #f0f2ee;
  white-space: pre-wrap;
}

.alert.inline {
  padding: 12px;
  border-radius: 8px;
}

.alert.good {
  background: var(--soft-good);
  color: var(--good);
}

.alert.bad {
  background: var(--soft-bad);
  color: var(--bad);
}

.bad-text {
  color: var(--bad);
}

.empty {
  padding: 28px 16px;
  border: 1px dashed #c7ccc5;
  border-radius: 8px;
  background: rgb(255 255 255 / 62%);
  text-align: center;
  color: var(--muted);
}

.event-row {
  display: grid;
  grid-template-columns: 16px 1fr;
  gap: 10px;
  align-items: start;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}

.event-row:last-child {
  border-bottom: 0;
}

.event-row p {
  margin: 2px 0;
}

.event-row small {
  color: var(--muted);
}

.dot {
  width: 10px;
  height: 10px;
  margin-top: 7px;
  border-radius: 999px;
  background: var(--muted);
}

.dot.high,
.dot.bad {
  background: var(--bad);
}

.dot.medium {
  background: var(--wait);
}

.chat-page {
  min-height: calc(100vh - 63px);
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.chat-list {
  min-height: 50vh;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-bottom: 12px;
}

.chat-bubble {
  width: min(82%, 540px);
  padding: 10px 12px;
  border-radius: 8px;
  background: #fff;
  border: 1px solid var(--line);
}

.chat-bubble.own {
  align-self: flex-end;
  background: #e5f3ee;
  border-color: #cae3d9;
}

.chat-bubble p {
  margin: 0 0 4px;
}

.chat-bubble small {
  color: var(--muted);
}

.inline-ref {
  display: inline-flex;
  border-radius: 6px;
  padding: 0 5px;
  background: #dfeae5;
  color: var(--brand-dark);
  font-weight: 760;
}

.chat-compose {
  position: sticky;
  bottom: 0;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 10px 0 0;
  background: var(--bg);
}

.helper {
  margin-bottom: 0;
  font-size: 13px;
}

@media (min-width: 760px) {
  .page,
  .chat-page {
    padding: 26px 24px 56px;
  }

  .detail-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .detail-grid .page-head,
  .detail-grid .alert,
  .detail-grid .media-panel {
    grid-column: 1 / -1;
  }

  .grid-form {
    grid-template-columns: repeat(4, minmax(0, 1fr)) auto;
    align-items: end;
  }

  .manual-image-form .wide-field,
  .manual-image-form .full-field {
    grid-column: 1 / -2;
  }
}

@media (max-width: 430px) {
  .page-head {
    align-items: flex-start;
  }

  .head-actions {
    align-items: flex-end;
    flex-direction: column;
  }

  .page-head h1 {
    font-size: 24px;
  }

  .stats-strip {
    grid-template-columns: 1fr;
  }

  .request-group-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .search-row {
    grid-template-columns: 1fr;
  }

  .facts div {
    grid-template-columns: 1fr;
    gap: 3px;
  }
}

@keyframes liveFlash {
  0% {
    border-color: #62b890;
    box-shadow: 0 0 0 4px rgb(31 122 77 / 18%);
  }
  100% {
    border-color: var(--line);
    box-shadow: 0 2px 10px rgb(32 33 36 / 4%);
  }
}

@keyframes countPop {
  0% {
    transform: scale(1);
  }
  45% {
    transform: scale(1.18);
  }
  100% {
    transform: scale(1);
  }
}
