:root {
  --bg: #f5f3ef;
  --surface: #ffffff;
  --sunken: #efebe5;
  --text: #2a251f;
  --muted: #7a7065;
  --line: #e3ddd4;
  --accent: #c2541b;
  --accent-dark: #a54513;
  --accent-soft: #fcede3;
  --ok: #2f7d4f;
  --error: #b3261e;
  --radius: 14px;
  --sidebar-w: 270px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html { font-size: 17px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Be Vietnam Pro', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  line-height: 1.45;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-weight: 400; }
.ok { color: var(--ok); }
button { font: inherit; color: inherit; }

/* ---------- Bố cục chính: vừa khít 1 màn hình ---------- */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ---------- Thanh bên ---------- */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  min-height: 0;
}
.brand { font-weight: 700; font-size: 1.25rem; padding: 0 8px 18px; }
.brand.big { font-size: 1.6rem; text-align: center; padding: 0 0 8px; }
.nav-title {
  font-size: .75rem; font-weight: 600; letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted); padding: 0 8px 8px;
}
.nav { display: flex; flex-direction: column; gap: 4px; overflow-y: auto; min-height: 0; }
.nav a {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 12px;
  border-radius: 12px;
  color: var(--text);
  text-decoration: none;
  border: 1px solid transparent;
  transition: background .12s;
}
.nav a:hover { background: var(--sunken); }
.nav a.active { background: var(--accent-soft); border-color: #f3cdb4; }
.nav a.active .nav-name { color: var(--accent-dark); }
.nav-icon { font-size: 1.5rem; width: 32px; text-align: center; flex: none; }
.nav-name { font-weight: 600; display: block; }
.nav-desc {
  font-size: .78rem; color: var(--muted); line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.nav-dot { margin-left: auto; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: none; }
.nav-dot.busy { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }
.nav-extra { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); flex: none; overflow: visible; }

.sidebar-foot {
  margin-top: auto;
  padding-top: 12px;
  display: grid; gap: 10px;
  font-size: .85rem;
  color: var(--muted);
}
.sidebar-foot > div { background: var(--sunken); border-radius: 12px; padding: 12px 14px; }
.sidebar-foot b { color: var(--text); }
.sidebar-foot ol { margin: 6px 0 0; padding-left: 18px; }
.usage-row { display: flex; justify-content: space-between; gap: 8px; margin-top: 4px; }
.usage-sub { margin-top: 4px; font-size: .78rem; }

/* ---------- Khu làm việc ---------- */
.work {
  display: grid;
  min-width: 0;
  min-height: 0;
  padding: 18px 22px;
}
.view { display: grid; gap: 14px; min-height: 0; min-width: 0; }
.view.editor { grid-template-rows: auto 1fr auto; }
.view.history { grid-template-rows: auto 1fr; }

/* Trang lịch sử */
.history-scroll { overflow-y: auto; min-height: 0; padding: 2px 4px 8px 0; }
.history-scroll > .empty { margin: 60px auto; }
.history-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; }
.h-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; display: grid; grid-template-rows: auto 1fr auto;
}
.h-thumb {
  border: none; padding: 0; cursor: zoom-in; display: block;
  aspect-ratio: 3 / 4; background: var(--sunken);
}
.h-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform .2s; }
.h-thumb:hover img { transform: scale(1.03); }
.h-info { padding: 10px 12px 4px; }
.h-name { font-weight: 600; }
.h-meta { color: var(--muted); font-size: .8rem; }
.h-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 8px 10px 10px; }
.btn.small { padding: 7px 8px; font-size: .85rem; border-radius: 8px; }

.work-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px 24px; flex-wrap: wrap;
}
.title-box { display: flex; align-items: center; gap: 14px; }
.title-icon {
  font-size: 1.9rem; width: 54px; height: 54px; display: grid; place-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
}
.work-head h1 { margin: 0; font-size: 1.45rem; }
.work-head p { margin: 0; color: var(--muted); }

.options { display: flex; flex-wrap: wrap; gap: 10px 18px; }
.option { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.option-label { font-weight: 600; font-size: .9rem; color: var(--muted); }
.seg {
  display: inline-flex; flex-wrap: wrap;
  background: var(--sunken); border-radius: 11px; padding: 3px; gap: 2px;
}
.seg button {
  border: none; background: none; cursor: pointer;
  padding: 7px 14px; border-radius: 9px; font-weight: 500; font-size: .92rem;
}
.seg button:hover { background: #fff8; }
.seg button.active { background: var(--surface); color: var(--accent-dark); font-weight: 600; box-shadow: 0 1px 3px #0002; }

/* Hai khung ảnh */
.stage {
  display: grid; grid-template-columns: 1fr 1fr; gap: 14px;
  min-height: 0;
}
.pane {
  display: grid; grid-template-rows: auto 1fr;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  min-height: 0; overflow: hidden;
}
.pane-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  font-weight: 600; font-size: .92rem;
}
.pane-body {
  position: relative;
  min-height: 0;
  display: grid; place-items: center;
  padding: 12px;
  background:
    linear-gradient(45deg, #f7f5f2 25%, transparent 25%) 0 0 / 20px 20px,
    linear-gradient(-45deg, #f7f5f2 25%, transparent 25%) 0 0 / 20px 20px,
    #fbfaf8;
}
.photo {
  max-width: calc(100% - 24px); max-height: calc(100% - 24px);
  width: auto; height: auto;
  object-fit: contain;
  border-radius: 6px;
  box-shadow: 0 2px 12px #0002;
  position: absolute; inset: 12px; margin: auto;
}
.zoomable { cursor: zoom-in; }

.drop { cursor: pointer; transition: background .15s; }
.drop input { display: none; }
.drop:hover .empty, .drop.dragging .empty { border-color: var(--accent); background: var(--accent-soft); }
.drop.dragging { outline: 3px dashed var(--accent); outline-offset: -8px; }

.empty {
  text-align: center; padding: 28px; border-radius: 14px;
  border: 2px dashed transparent;
  max-width: 340px;
}
.drop .empty { border-color: var(--line); width: min(100%, 360px); padding: 40px 24px; }
.empty-icon { font-size: 2.6rem; }
.empty-title { font-weight: 700; font-size: 1.1rem; }
.empty-sub { color: var(--muted); }
.error .empty-sub { color: var(--error); }

.spinner {
  width: 48px; height: 48px; margin: 0 auto 12px;
  border: 5px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.progress { width: 220px; max-width: 100%; height: 6px; margin: 14px auto 0; background: var(--line); border-radius: 99px; overflow: hidden; }
#progress-bar { height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .5s linear; }
.spinner.small { width: 32px; height: 32px; border-width: 4px; }
.small { font-size: .82rem; }
.timer { margin-top: 8px; font-variant-numeric: tabular-nums; color: var(--muted); font-size: .9rem; }

/* Thanh hành động */
.actionbar {
  display: flex; gap: 12px; align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
}
.note {
  flex: 1; min-width: 0;
  font: inherit; color: inherit;
  border: 1px solid var(--line); background: var(--bg);
  border-radius: 10px; padding: 11px 14px;
}
.note:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--surface); }
.actions { display: flex; gap: 10px; flex: none; }
.seg.quality { flex: none; }
.seg.quality button { display: grid; line-height: 1.15; padding: 5px 12px; text-align: center; }
.seg.quality small { font-size: .72rem; color: var(--muted); font-weight: 500; }

/* Viền khi dùng bàn phím (Tab) */
:focus-visible { outline: 3px solid #e8a37b; outline-offset: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font-weight: 600; font-size: 1rem;
  padding: 11px 20px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); color: var(--accent-dark); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); color: #fff; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
#go { min-width: 160px; font-size: 1.05rem; }
.link { border: none; background: none; color: var(--accent); cursor: pointer; font-weight: 600; font-size: .9rem; padding: 0; }

/* Xem phóng to */
.viewer {
  position: fixed; inset: 0; z-index: 50;
  background: #15120fee;
  overflow: auto;
  overscroll-behavior: contain;
}
.viewer-stage {
  display: flex;
  min-width: 100%; min-height: 100%;
  width: max-content;
  padding: 24px 24px 96px;
}
.viewer img {
  display: block;
  margin: auto; /* căn giữa nhưng vẫn cuộn được tới mép khi ảnh to hơn màn hình */
  max-width: calc(100vw - 48px);
  max-height: calc(100vh - 120px);
  max-height: calc(100dvh - 120px);
  cursor: zoom-in;
  user-select: none; -webkit-user-drag: none;
  box-shadow: 0 8px 40px #0008;
}
.viewer.zoomed img { max-width: none; max-height: none; cursor: grab; }
.viewer.zoomed.dragging img { cursor: grabbing; }
.viewer-bar { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); display: flex; gap: 8px; }
.viewer-bar .btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.viewer-tip {
  position: fixed; top: 14px; left: 50%; transform: translateX(-50%);
  background: #0009; color: #fff; font-size: .85rem; padding: 6px 14px; border-radius: 999px;
  pointer-events: none;
}

.toast {
  position: fixed; bottom: 96px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 12px 20px; border-radius: 12px;
  z-index: 60; box-shadow: 0 6px 20px #0003; max-width: calc(100vw - 32px);
}

/* Mật khẩu */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-box {
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px;
  padding: 28px; width: min(100%, 380px); display: grid; gap: 12px;
}
.login-box input {
  font: inherit; border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: var(--bg);
}
.error-text { color: var(--error); margin: 0; }

/* ---------- Màn hình thấp (laptop) ---------- */
@media (max-height: 900px) and (min-width: 821px) {
  .howto { display: none; }
  .nav-desc { -webkit-line-clamp: 1; }
}
@media (max-height: 720px) and (min-width: 821px) {
  .nav-desc { display: none; }
}

/* ---------- Màn hình nhỏ / điện thoại ---------- */
@media (max-width: 1100px) {
  :root { --sidebar-w: 220px; }
  .nav-desc { display: none; }
}
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; height: auto; min-height: 100dvh; overflow: visible; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--line); padding: 12px 16px 10px; }
  .brand { padding: 0 0 10px; }
  .nav-title, .sidebar-foot { display: none; }
  .sidebar { display: grid; grid-template-columns: 1fr auto; column-gap: 8px; }
  .brand { grid-column: 1 / -1; }
  .nav { flex-direction: row; overflow-x: auto; padding-bottom: 4px; }
  .nav-extra { margin: 0; padding: 0; border: none; }
  .nav-extra .nav-desc { display: none; }
  .view.history { grid-template-rows: auto auto; }
  .history-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .h-actions { grid-template-columns: 1fr; }
  .seg.quality { align-self: center; }
  .nav a { flex: none; padding: 8px 12px; gap: 6px; border-color: var(--line); }
  .nav-icon { font-size: 1.15rem; width: auto; }
  .work { padding: 14px 16px; }
  .stage { grid-template-columns: 1fr; }
  .pane-body { height: 46vh; }
  .actionbar { flex-wrap: wrap; position: sticky; bottom: 8px; box-shadow: 0 -4px 20px #0001; gap: 8px; }
  .note { flex-basis: 100%; padding: 9px 12px; }
  .actions { flex: 1; min-width: 0; }
  .actions .btn { flex: 1; padding: 10px 8px; min-width: 0; }
  #go { min-width: 0; }
  .nav-extra a > span:last-child { display: none; }
}
