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

:root {
  --bg: #f0f4f8;
  --surface: #ffffff;
  --surface2: #f8fafc;
  --border: #e2e8f0;
  --border-focus: #3b82f6;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-light: #94a3b8;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-light: #eff6ff;
  --secondary: #475569;
  --secondary-hover: #334155;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-light: #fef2f2;
  --success: #16a34a;
  --success-light: #f0fdf4;
  --green: #22c55e;
  --orange: #f59e0b;
  --red: #ef4444;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
  --shadow-md: 0 4px 12px rgba(0,0,0,.08), 0 2px 4px rgba(0,0,0,.05);
}

html {
  font-size: 16px;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding: 1rem 0 3rem;
  -webkit-font-smoothing: antialiased;
}

/* Layout */
.container {
  max-width: 820px;
  margin: 0 auto;
  padding: 0 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1.5rem 0 0.5rem;
}

.header-icon {
  font-size: 2.5rem;
  line-height: 1;
}

.app-header h1 {
  font-size: 1.625rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text);
}

.app-header p {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Card */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.card-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
  padding-bottom: 0.875rem;
  border-bottom: 1px solid var(--border);
}

/* Upload Area */
.upload-area {
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  padding: 2rem 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  position: relative;
  margin-bottom: 1rem;
}

.upload-area:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.upload-area.drag-over {
  border-color: var(--primary);
  background: var(--primary-light);
}

.upload-area input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}

.upload-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
}

.upload-main {
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 0.25rem;
}

.upload-hint {
  font-size: 0.8125rem;
  color: var(--text-muted);
}

/* Image Grid */
.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 0.625rem;
  margin-bottom: 1rem;
}

.image-thumb {
  position: relative;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 2px solid var(--border);
  background: var(--surface2);
}

.image-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.image-thumb .remove-btn {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(0,0,0,0.65);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 0.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  transition: background 0.15s;
}

.image-thumb .remove-btn:hover {
  background: var(--danger);
}

.image-thumb .ocr-badge {
  position: absolute;
  bottom: 3px;
  left: 3px;
  font-size: 0.65rem;
  font-weight: 600;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--success);
  color: #fff;
}

.image-thumb .ocr-badge.processing {
  background: var(--orange);
}

/* OCR Status */
.ocr-status {
  margin-bottom: 1rem;
}

.progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 0.5rem;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 3px;
  transition: width 0.3s ease;
  width: 0%;
}

.ocr-text {
  font-size: 0.8125rem;
  color: var(--text-muted);
  text-align: center;
}

/* Button Row */
.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.625rem;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.btn-primary:not(:disabled):hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: var(--surface2);
  border-color: #cbd5e1;
}

.btn-danger {
  background: var(--danger-light);
  color: var(--danger);
  border-color: #fecaca;
}

.btn-danger:hover {
  background: #fee2e2;
  border-color: #fca5a5;
}

.btn-large {
  padding: 0.75rem 2rem;
  font-size: 1rem;
  font-weight: 600;
  width: 100%;
  justify-content: center;
}

/* Table */
.table-scroll {
  overflow-x: auto;
  margin-bottom: 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

thead {
  background: var(--surface2);
}

th {
  padding: 0.75rem 0.875rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.8125rem;
  color: var(--text-muted);
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}

td {
  padding: 0.5rem 0.625rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tbody tr:last-child td {
  border-bottom: none;
}

tbody tr:hover {
  background: #fafbfc;
}

.empty-cell {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.875rem;
  padding: 2rem;
}

/* Table Inputs */
.table-input {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  padding: 0.3rem 0.4rem;
  border-radius: 6px;
  font-size: 0.875rem;
  font-family: inherit;
  color: var(--text);
  transition: border-color 0.15s, background 0.15s;
  min-width: 0;
}

.table-input:focus {
  outline: none;
  border-color: var(--primary);
  background: var(--primary-light);
}

.table-input.num-input {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.table-input::placeholder {
  color: var(--text-light);
}

.delete-row-btn {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-light);
  cursor: pointer;
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  margin: 0 auto;
}

.delete-row-btn:hover {
  background: var(--danger-light);
  color: var(--danger);
  border-color: #fecaca;
}

/* Settings */
.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1.25rem;
}

.setting-label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.optional-hint {
  font-size: 0.7rem;
  font-weight: 400;
  color: var(--text-light);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  margin-left: 4px;
  vertical-align: middle;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.setting-hint {
  font-size: 0.75rem;
  color: var(--text-light);
  margin-top: 0.375rem;
}

.percent-row {
  display: flex;
  gap: 0.375rem;
}

.pct-btn {
  flex: 1;
  padding: 0.5rem 0;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9375rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
}

.pct-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.pct-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.euro-input {
  display: flex;
  align-items: center;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface);
  transition: border-color 0.15s;
}

.euro-input:focus-within {
  border-color: var(--primary);
}

.euro-input span {
  padding: 0 0.625rem;
  color: var(--text-muted);
  font-size: 0.875rem;
  font-weight: 500;
  background: var(--surface2);
  border-right: 1.5px solid var(--border);
  height: 100%;
  display: flex;
  align-items: center;
  align-self: stretch;
}

.euro-input input {
  flex: 1;
  border: none;
  outline: none;
  padding: 0.5rem 0.625rem;
  font-size: 0.9375rem;
  font-family: inherit;
  color: var(--text);
  background: transparent;
  font-variant-numeric: tabular-nums;
  min-width: 0;
}

/* Results */
.result-card {
  border: 2px solid #dbeafe;
  background: linear-gradient(135deg, #eff6ff 0%, #f0fdf4 100%);
}

.result-grid {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.result-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 0;
  border-bottom: 1px solid rgba(0,0,0,0.06);
}

.result-row:last-child {
  border-bottom: none;
}

.result-row.deduction {
  opacity: 0.85;
}

.result-label {
  font-size: 0.9375rem;
  color: var(--text-muted);
}

.result-val {
  font-size: 1.0625rem;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.result-val.neg {
  color: var(--danger);
}

.result-divider {
  height: 1px;
  background: rgba(0,0,0,0.1);
  margin: 0.25rem 0;
}

.final-row {
  padding: 1rem 0 0.25rem;
}

.final-label {
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--text);
}

.final-val {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--success);
  letter-spacing: -0.5px;
}

.final-val.negative {
  color: var(--danger);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #1e293b;
  color: #fff;
  padding: 0.75rem 1.5rem;
  border-radius: 50px;
  font-size: 0.875rem;
  font-weight: 500;
  z-index: 9999;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  white-space: nowrap;
  max-width: calc(100vw - 2rem);
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Responsive */
@media (max-width: 600px) {
  .app-header h1 {
    font-size: 1.3rem;
  }

  .header-icon {
    font-size: 2rem;
  }

  .card {
    padding: 1.125rem 1rem;
  }

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

  th, td {
    padding: 0.5rem 0.5rem;
    font-size: 0.8125rem;
  }

  .final-val {
    font-size: 1.5rem;
  }

  .result-label {
    font-size: 0.875rem;
  }

  .result-val {
    font-size: 0.9375rem;
  }
}
