:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --green: #16a34a;
  --red: #dc2626;
}

* { box-sizing: border-box; }

/* The HTML 'hidden' attribute must always win, even over display:flex/grid */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans Thai", sans-serif;
  background: var(--bg);
  color: var(--ink);
}

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
}
.login-card {
  background: var(--card); padding: 36px 32px; border-radius: 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,.08); width: 340px; text-align: center;
}
.login-card h1 { margin: 0; font-size: 32px; }
.subtitle { color: var(--muted); margin: 4px 0 22px; }
#login-form { display: flex; flex-direction: column; gap: 14px; text-align: left; }
#login-form label { font-size: 13px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); padding: 12px 20px; border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; font-size: 18px; }
.spacer { flex: 1; }
#who { color: var(--muted); font-size: 14px; }

/* ---------- Layout ---------- */
.container { max-width: 980px; margin: 0 auto; padding: 24px 16px 60px; }
h2 { margin: 8px 0 16px; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 18px 18px; margin-bottom: 18px;
}
.card h3 { margin: 0 0 12px; font-size: 16px; }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0; }
.tag {
  background: #eff6ff; color: var(--brand); font-size: 11px; padding: 2px 8px;
  border-radius: 999px; margin-left: 6px; vertical-align: middle;
}

/* ---------- Forms ---------- */
input, select {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 14px; background: #fff; color: var(--ink);
}
input:focus, select:focus { outline: 2px solid #bfdbfe; border-color: var(--brand); }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 8px 0; }

button {
  padding: 9px 14px; border: none; border-radius: 8px; cursor: pointer;
  background: var(--brand); color: #fff; font-size: 14px; font-weight: 600;
}
button:hover { background: var(--brand-dark); }
button.ghost { background: #eef2f7; color: var(--ink); }
button.ghost:hover { background: #e2e8f0; }
button.primary { background: var(--green); }
button.primary:hover { background: #15803d; }
button.danger { background: var(--red); padding: 6px 10px; font-size: 12px; }

.filebtn {
  display: inline-block; padding: 9px 14px; border-radius: 8px; cursor: pointer;
  background: #eef2f7; font-size: 14px; font-weight: 600;
}
.filebtn:hover { background: #e2e8f0; }

/* ---------- Lists ---------- */
.list { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.list-item {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; font-size: 14px;
}
.list-item .name { font-weight: 600; }
.list-item .meta { color: var(--muted); font-size: 12px; }
.list-item .spacer { flex: 1; }

/* ---------- Product table ---------- */
.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; }
#prod-table { border-collapse: collapse; width: 100%; font-size: 13px; }
#prod-table th, #prod-table td { border: 1px solid var(--line); padding: 0; }
#prod-table th { background: #f8fafc; padding: 8px 6px; white-space: nowrap; position: sticky; top: 0; }
#prod-table td input { border: none; border-radius: 0; width: 130px; padding: 7px 8px; }
#prod-table td.rowdel { width: 36px; text-align: center; }

/* ---------- Misc ---------- */
code { background: #f1f5f9; padding: 2px 6px; border-radius: 5px; font-size: 13px; }
.current { color: var(--green); font-size: 13px; font-weight: 600; }
.preview {
  background: #0f172a; color: #e2e8f0; padding: 12px; border-radius: 8px;
  max-height: 160px; overflow: auto; white-space: pre-wrap; font-size: 12px; margin-top: 10px;
}
.preview:empty { display: none; }
.answer {
  background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 8px;
  padding: 12px; margin-top: 10px; white-space: pre-wrap; min-height: 20px;
}
.answer:empty { display: none; }
.msg { font-size: 13px; margin: 8px 0 0; }
.msg.ok { color: var(--green); }
.msg.err { color: var(--red); }
.error { color: var(--red); font-size: 13px; min-height: 16px; }

/* ---------- Strategy / Priority forms ---------- */
.docform { margin: 10px 0; display: flex; flex-direction: column; gap: 12px; }
.docfield { display: flex; flex-direction: column; gap: 4px; }
.docfield label { font-weight: 600; font-size: 14px; }
.docfield .fhint { color: var(--muted); font-size: 12px; }
.docfield textarea {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px;
  font-family: inherit; font-size: 14px; min-height: 54px; resize: vertical;
}
.docfield textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--brand); }
.order-row { display: flex; align-items: center; gap: 8px; margin: 4px 0; }
.order-row .num { width: 22px; color: var(--muted); font-weight: 700; }
.order-row select { flex: 1; }
details { margin-top: 8px; }
summary { cursor: pointer; }

/* ---------- API key display ---------- */
.keyline {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin-top: 4px; font-size: 12px; color: var(--muted);
}
.keyline code { word-break: break-all; background: #f1f5f9; color: var(--ink); }
.copybtn { padding: 3px 10px; font-size: 12px; }

/* ---------- Tiny toast ---------- */
#tiny-toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: #0f172a; color: #fff; padding: 9px 16px; border-radius: 8px;
  font-size: 13px; opacity: 0; pointer-events: none; transition: opacity .2s;
  z-index: 50;
}
#tiny-toast.show { opacity: 1; }
