/*
 * AiDOCU frontend stylesheet. Hand-written, no build step. Layout "A": a 240 px sidebar and a main column whose
 * result view splits into the document viewer and the data panel, each scrolling on its own.
 *
 * State conventions toggled by app.js:
 *   [hidden]           hides any element (upload view, result view, task header, panels, notice)
 *   .is-open           on #sidebar: shows the off-canvas sidebar below 1024 px
 *   .is-selected       on a .nav-item or .history-item: the current document type / open entry
 *   .is-dragover       on #drop-zone: a file is dragged over it
 */

/* ---------- Tokens ---------- */

:root {
  --green: #009840;
  --green-dark: #007a33;
  --green-tint: #e8f5ee;
  --green-tint-soft: #f0faf4;

  --grey-50: #f9fafb;
  --grey-100: #f3f4f6;
  --grey-200: #e5e7eb;
  --grey-300: #d1d5db;
  --grey-400: #9ca3af;
  --grey-500: #6b7280;
  --grey-600: #4b5563;
  --grey-700: #374151;
  --grey-800: #1f2937;
  --grey-900: #111827;

  --red-50: #fef2f2;
  --red-100: #fee2e2;
  --red-600: #dc2626;
  --red-800: #991b1b;
  --amber-600: #d97706;
  --yellow-100: #fef9c3;
  --yellow-800: #854d0e;
  --green-100: #dcfce7;
  --green-800: #166534;

  --viewer-bg: #525659;
  --tooltip-bg: var(--grey-800);

  --radius-sm: 6px;
  --radius: 8px;
  --radius-lg: 16px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-6: 24px;
  --space-8: 32px;

  --sidebar-width: 240px;
  --topbar-height: 56px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --shadow-card: 0 10px 25px rgba(17, 24, 39, 0.08), 0 2px 6px rgba(17, 24, 39, 0.05);
}

/* ---------- Base ---------- */

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--grey-800);
  background: var(--grey-100);
}

h1,
h2,
h3,
h4,
p {
  margin: 0;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--green);
  outline-offset: 2px;
}

.muted {
  color: var(--grey-500);
}

/* ---------- Buttons ---------- */

.btn-primary,
.btn-ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  border-radius: var(--radius);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.btn-primary {
  padding: 10px var(--space-4);
  border: 1px solid transparent;
  background: var(--green);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--green-dark);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-block {
  width: 100%;
}

.btn-ghost {
  padding: 5px 10px;
  border: 1px solid var(--grey-200);
  background: #fff;
  color: var(--grey-600);
  font-weight: 500;
  font-size: 13px;
}

.btn-ghost:hover {
  background: var(--grey-50);
  color: var(--grey-800);
}

.btn-ghost--danger:hover {
  border-color: var(--red-100);
  background: var(--red-50);
  color: var(--red-600);
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-1);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--grey-500);
}

.icon-button:hover {
  background: var(--grey-100);
  color: var(--grey-800);
}

/* ---------- App shell ---------- */

.app-body {
  overflow: hidden;
}

.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Sidebar ---------- */

.sidebar {
  display: flex;
  flex: 0 0 var(--sidebar-width);
  flex-direction: column;
  width: var(--sidebar-width);
  min-height: 0;
  background: #fff;
  border-right: 1px solid var(--grey-200);
}

.sidebar-logo {
  padding: 18px var(--space-4) var(--space-3);
}

.sidebar-logo img {
  display: block;
  height: 22px;
  width: auto;
}

.sidebar-new {
  margin: var(--space-1) var(--space-3) var(--space-3);
}

.sidebar-label {
  padding: var(--space-3) var(--space-4) var(--space-1);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--grey-400);
}

.nav {
  display: flex;
  flex-direction: column;
}

.nav-item {
  display: block;
  width: 100%;
  padding: 7px var(--space-4);
  border: 0;
  border-left: 3px solid transparent;
  background: transparent;
  color: var(--grey-700);
  text-align: left;
}

.nav-item:hover {
  background: var(--grey-50);
}

.nav-item.is-selected {
  border-left-color: var(--green);
  background: var(--green-tint);
  color: var(--green-dark);
  font-weight: 600;
}

.history-notice {
  margin: var(--space-1) var(--space-4) var(--space-2);
  padding: 6px var(--space-2);
  border-radius: var(--radius-sm);
  background: var(--grey-100);
  color: var(--grey-500);
  font-size: 12px;
}

.history-list {
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
}

.sidebar-footer {
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--grey-200);
}

.sidebar-footer a {
  color: var(--grey-500);
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
}

.sidebar-footer a:hover {
  color: var(--red-600);
}

/* ---------- History item ---------- */

.history-item {
  position: relative;
  border-bottom: 1px solid var(--grey-100);
}

.history-item:hover {
  background: var(--grey-50);
}

.history-item.is-selected {
  background: var(--green-tint-soft);
}

.history-item__open {
  display: block;
  width: 100%;
  padding: 8px var(--space-4);
  border: 0;
  background: transparent;
  text-align: left;
}

.history-item__name {
  display: block;
  padding-right: 22px;
  overflow: hidden;
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.history-item__meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-top: 2px;
  color: var(--grey-500);
  font-size: 12px;
}

.history-item__meta-text {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.history-item__delete {
  position: absolute;
  top: 6px;
  right: var(--space-2);
  opacity: 0;
  color: var(--grey-400);
}

.history-item:hover .history-item__delete,
.history-item:focus-within .history-item__delete {
  opacity: 1;
}

/* Touch screens have no hover: keep the trash button visible. */
@media (hover: none) {
  .history-item__delete {
    opacity: 1;
  }
}

.history-item__delete:hover {
  background: var(--red-50);
  color: var(--red-600);
}

/* ---------- Status chips ---------- */

.chip {
  flex: none;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.5;
}

.chip--done {
  background: var(--green-100);
  color: var(--green-800);
}

.chip--running {
  background: var(--yellow-100);
  color: var(--yellow-800);
}

.chip--error {
  background: var(--red-100);
  color: var(--red-800);
}

/* ---------- Main column ---------- */

.main {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.topbar {
  display: flex;
  flex: none;
  align-items: center;
  gap: var(--space-3);
  height: var(--topbar-height);
  padding: 0 var(--space-4);
  background: #fff;
  border-bottom: 1px solid var(--grey-200);
}

.menu-toggle {
  display: none;
}

.main-title {
  flex: none;
  font-size: 16px;
  font-weight: 700;
  color: var(--grey-900);
  white-space: nowrap;
}

.task-header {
  display: flex;
  flex: 1;
  align-items: baseline;
  gap: var(--space-3);
  min-width: 0;
}

.task-filename {
  overflow: hidden;
  color: var(--grey-700);
  font-weight: 500;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.task-meta {
  flex: none;
  color: var(--grey-500);
  font-size: 12px;
}

.task-header .btn-ghost {
  align-self: center;
  margin-left: auto;
}

/* ---------- Upload view ---------- */

.upload-view {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-8) var(--space-6);
}

.upload-card {
  max-width: 640px;
  margin: 0 auto;
  padding: var(--space-6);
  border: 1px solid var(--grey-200);
  border-radius: var(--radius-lg);
  background: #fff;
}

.drop-zone {
  padding: var(--space-8) var(--space-6);
  border: 2px dashed var(--grey-300);
  border-radius: var(--radius);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.drop-zone:hover {
  border-color: var(--grey-400);
}

.drop-zone.is-dragover {
  border-color: var(--green);
  background: var(--green-tint);
}

.drop-zone-icon {
  display: block;
  margin: 0 auto var(--space-3);
  color: var(--grey-400);
}

.drop-zone-text {
  color: var(--grey-600);
  font-weight: 500;
}

.drop-zone-hint {
  margin-top: var(--space-1);
  color: var(--grey-500);
  font-size: 13px;
}

.text-accent {
  color: var(--green);
  font-weight: 600;
}

.file-name {
  margin-top: var(--space-3);
  overflow-wrap: anywhere;
  color: var(--grey-800);
  font-weight: 600;
}

.file-name:empty {
  display: none;
}

.options-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  margin-top: var(--space-4);
  padding: var(--space-4);
  border: 1px solid var(--grey-200);
  border-radius: var(--radius);
  background: var(--grey-50);
}

.options-title {
  font-weight: 600;
}

.options-hint {
  margin-top: 2px;
  color: var(--grey-500);
  font-size: 13px;
}

.upload-card .btn-primary {
  margin-top: var(--space-4);
}

/* ---------- Switch ---------- */

.switch {
  position: relative;
  display: inline-block;
  flex: none;
  width: 44px;
  height: 24px;
}

.switch input {
  position: absolute;
  width: 0;
  height: 0;
  opacity: 0;
}

.switch-slider {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: var(--grey-300);
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.switch-slider::before {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  content: "";
  transition: transform 0.2s ease;
}

.switch input:checked + .switch-slider {
  background: var(--green);
}

.switch input:checked + .switch-slider::before {
  transform: translateX(20px);
}

.switch input:focus-visible + .switch-slider {
  outline: 2px solid var(--green);
  outline-offset: 2px;
}

/* ---------- Result view (split) ---------- */

.result-view {
  display: flex;
  flex: 1;
  gap: var(--space-3);
  min-height: 0;
  padding: var(--space-3);
}

.viewer {
  display: flex;
  flex: 1 1 0;
  align-items: stretch;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  border-radius: var(--radius);
  background: var(--viewer-bg);
}

.viewer iframe {
  flex: 1;
  width: 100%;
  min-height: 100%;
  border: 0;
}

.viewer img {
  align-self: flex-start;
  max-width: 100%;
  height: auto;
  margin: var(--space-4);
  background: #fff;
}

/* Zoomable image viewer (static/image_viewer.js): the stage clips, the image is positioned by transform. */
.viewer:has(.image-stage) {
  position: relative;
  overflow: hidden;
}

.image-stage {
  position: relative;
  flex: 1;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.image-stage.is-panning {
  cursor: grabbing;
}

.image-stage:focus-visible {
  outline: 2px solid var(--green);
  outline-offset: -2px;
}

.viewer .image-stage img {
  position: absolute;
  top: 0;
  left: 0;
  max-width: none;
  margin: 0;
  transform-origin: 0 0;
  -webkit-user-drag: none;
}

.image-toolbar {
  position: absolute;
  top: var(--space-2);
  right: var(--space-2);
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  border-radius: var(--radius);
  background: rgba(32, 33, 36, 0.85);
  color: #fff;
  font-size: 0.8125rem;
}

.image-toolbar__button {
  min-width: 2rem;
  height: 1.75rem;
  padding: 0 var(--space-2);
  border: 0;
  border-radius: calc(var(--radius) - 2px);
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.image-toolbar__button:hover:not(:disabled),
.image-toolbar__button:focus-visible {
  background: rgba(255, 255, 255, 0.16);
}

.image-toolbar__button:disabled {
  opacity: 0.4;
  cursor: default;
}

.image-toolbar__level {
  min-width: 3.25rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.viewer-message {
  align-self: center;
  padding: var(--space-6);
  color: var(--grey-200);
  text-align: center;
}

.viewer-message a {
  color: #fff;
  font-weight: 600;
}

.data-panel {
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  padding: var(--space-4) 18px;
  border: 1px solid var(--grey-200);
  border-radius: var(--radius);
  background: #fff;
}

.processing {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-4);
  min-height: 100%;
  color: var(--grey-600);
  font-weight: 500;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--grey-200);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.error-box {
  padding: var(--space-4);
  border: 1px solid var(--red-100);
  border-radius: var(--radius);
  background: var(--red-50);
  color: var(--red-600);
  overflow-wrap: anywhere;
}

/* ---------- Result content (classes emitted by render.js) ---------- */

.section-title {
  margin: 22px 0 var(--space-2);
  color: var(--green);
  font-size: 14px;
  font-weight: 700;
}

.result-content > .section-title:first-child {
  margin-top: 0;
}

.subsection-title {
  margin: var(--space-4) 0 var(--space-2);
  color: var(--grey-700);
  font-size: 13px;
  font-weight: 600;
}

.data-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  padding: 7px 0;
  border-bottom: 1px solid var(--grey-100);
}

.data-label {
  flex: 0 1 auto;
  color: var(--grey-500);
  font-weight: 500;
}

.data-value {
  display: inline-flex;
  flex: 1 1 auto;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  min-width: 0;
  color: var(--grey-800);
  font-weight: 600;
  text-align: right;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.data-value--total {
  color: var(--grey-900);
  font-size: 16px;
  font-weight: 700;
}

.data-table {
  width: 100%;
  margin: var(--space-1) 0 var(--space-2);
  border-collapse: collapse;
  font-size: 13px;
}

.data-table th {
  padding: 6px var(--space-2);
  background: var(--green-tint-soft);
  color: var(--green-dark);
  font-weight: 600;
  text-align: left;
  vertical-align: bottom;
}

.data-table td {
  padding: 6px var(--space-2);
  border-bottom: 1px solid var(--grey-100);
  vertical-align: top;
  overflow-wrap: break-word;
}

.data-table .cell-right {
  text-align: right;
}

.cell-strong {
  font-weight: 600;
}

.data-table .cell-nowrap {
  white-space: nowrap;
}

.dropdown {
  margin: var(--space-2) 0;
  border: 1px solid var(--grey-200);
  border-radius: var(--radius);
}

.dropdown > summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2) var(--space-3);
  font-weight: 600;
  list-style: none;
  cursor: pointer;
}

.dropdown > summary::-webkit-details-marker {
  display: none;
}

.dropdown > summary:hover {
  background: var(--grey-50);
}

.dropdown > summary > svg {
  flex: none;
  color: var(--grey-500);
  transition: transform 0.2s ease;
}

.dropdown[open] > summary > svg {
  transform: rotate(180deg);
}

.dropdown-body {
  padding: 0 var(--space-3) var(--space-2);
  overflow-x: auto;
}

/* Validation icon with a pure-CSS tooltip, shown below the icon (a tooltip above the first rows would be clipped by
 * the data panel's scroll box, while one below only extends the scrollable area). */
.validation-icon {
  position: relative;
  display: inline-flex;
  flex: none;
  cursor: help;
}

.validation-icon--error {
  color: var(--red-600);
}

.validation-icon--warning {
  color: var(--amber-600);
}

.tooltip {
  position: absolute;
  top: calc(100% + 8px);
  right: -6px;
  z-index: 20;
  width: max-content;
  max-width: 280px;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  background: var(--tooltip-bg);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.18);
  color: #fff;
  font-size: 12px;
  font-weight: 400;
  line-height: 1.45;
  text-align: left;
  white-space: normal;
  visibility: hidden;
  opacity: 0;
  transition: opacity 0.15s ease, visibility 0.15s ease;
}

.tooltip::after {
  position: absolute;
  bottom: 100%;
  right: 10px;
  border: 6px solid transparent;
  border-bottom-color: var(--tooltip-bg);
  content: "";
}

.tooltip hr {
  margin: 6px 0;
  border: 0;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.validation-icon:hover .tooltip,
.validation-icon:focus-within .tooltip {
  visibility: visible;
  opacity: 1;
}

/* ---------- Login ---------- */

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: var(--space-4);
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: var(--space-8);
  border-radius: var(--radius-lg);
  background: #fff;
  box-shadow: var(--shadow-card);
}

.login-card img {
  display: block;
  height: 24px;
  width: auto;
  margin-bottom: var(--space-6);
}

.login-title {
  margin-bottom: var(--space-6);
  color: var(--grey-900);
  font-size: 22px;
  font-weight: 700;
}

.alert {
  margin-bottom: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  font-size: 13px;
}

.alert--error {
  background: var(--red-50);
  color: var(--red-600);
}

.alert--info {
  background: var(--green-tint);
  color: var(--green-dark);
}

.form-field {
  margin-bottom: var(--space-4);
}

.form-field label {
  display: block;
  margin-bottom: 6px;
  color: var(--grey-700);
  font-size: 13px;
  font-weight: 600;
}

.form-field input {
  width: 100%;
  padding: 10px var(--space-3);
  border: 1px solid var(--grey-300);
  border-radius: var(--radius);
  background: #fff;
}

.form-field input:focus {
  border-color: var(--green);
  outline: 0;
  box-shadow: 0 0 0 3px var(--green-tint);
}

/* ---------- Narrow screens (< 1024 px) ---------- */

@media (max-width: 1023.98px) {
  .menu-toggle {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    top: var(--topbar-height);
    bottom: 0;
    left: 0;
    z-index: 30;
    box-shadow: 6px 0 16px rgba(17, 24, 39, 0.08);
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.2s ease, visibility 0.2s ease;
  }

  .sidebar.is-open {
    transform: translateX(0);
    visibility: visible;
  }

  .result-view {
    flex-direction: column;
  }

  .viewer {
    flex: 0 0 42%;
  }

  .upload-view {
    padding: var(--space-4);
  }
}
