/* ============================================================================
   BASE STYLES & VARIABLES
   ============================================================================ */

:root {
  --bg-primary: #0f1115;
  --bg-secondary: #0b0d12;
  --bg-tertiary: #161b22;
  --border-color: #30363d;
  --text-primary: #e6edf3;
  --text-secondary: #9da7b3;
  --text-muted: #8b949e;
  --color-success: #2ea043;
  --color-danger: #da3633;
  --color-warning: #f9a825;
  --color-info: #58a6ff;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
}

/* ============================================================================
   LAYOUT
   ============================================================================ */

.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 24px;
}

.header {
  text-align: center;
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 2px solid var(--border-color);
}

.header h1 {
  margin: 0 0 8px 0;
  font-size: 32px;
  font-weight: 700;
  color: var(--text-primary);
}

.subtitle {
  margin: 0;
  font-size: 16px;
  color: var(--text-secondary);
  font-weight: 400;
}

/* ============================================================================
   CARDS & SECTIONS
   ============================================================================ */

.card {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}

.card h2 {
  margin-top: 0;
  margin-bottom: 16px;
  font-size: 18px;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
}

.card h3 {
  margin: 0 0 12px 0;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  font-weight: 600;
}

/* Input Section */
.input-section textarea {
  width: 100%;
  min-height: 160px;
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: "Monaco", "Menlo", "Ubuntu Mono", monospace;
  font-size: 13px;
  line-height: 1.5;
  resize: vertical;
  margin-bottom: 12px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-section textarea:focus {
  outline: none;
  border-color: var(--color-info);
  box-shadow: 0 0 0 3px rgba(88, 166, 255, 0.1);
}

.input-section textarea::placeholder {
  color: var(--text-muted);
}

/* Controls Section */
.controls-section {
  background: linear-gradient(135deg, var(--bg-tertiary) 0%, rgba(88, 166, 255, 0.05) 100%);
}

.section-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 24px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.button-group {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.button-group .btn {
  flex: 1;
  min-width: 120px;
}

.view-indicator {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 600;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
}

.view-indicator.raw {
  color: var(--color-info);
  border-color: var(--color-info);
}

.view-indicator.craftables {
  color: var(--color-success);
  border-color: var(--color-success);
}

/* Toggle Switch */
.toggle-switch {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.toggle-input {
  display: none;
}

.toggle-track {
  position: relative;
  display: inline-flex;
  width: 240px;
  height: 40px;
  background: var(--bg-secondary);
  border: 2px solid var(--border-color);
  border-radius: 20px;
  padding: 0;
  overflow: hidden;
  transition: border-color 0.3s ease;
}

.toggle-track::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 50%;
  height: calc(100% - 4px);
  background: var(--color-info);
  border-radius: 16px;
  transition: all 0.3s ease;
  z-index: 1;
}

.toggle-input:checked + .toggle-track {
  border-color: var(--color-success);
}

.toggle-input:checked + .toggle-track::before {
  left: calc(50% + 2px);
  background: var(--color-success);
}

.toggle-option {
  position: relative;
  flex: 1;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
  z-index: 2;
  transition: color 0.3s ease;
  color: var(--text-secondary);
}

.toggle-input + .toggle-track .toggle-option:first-child {
  color: white;
}

.toggle-input + .toggle-track .toggle-option:last-child {
  color: var(--text-secondary);
}

.toggle-input:checked + .toggle-track .toggle-option:first-child {
  color: var(--text-secondary);
}

.toggle-input:checked + .toggle-track .toggle-option:last-child {
  color: white;
}

/* Progress Section */
.progress-section {
  background: linear-gradient(135deg, var(--bg-tertiary) 0%, rgba(46, 160, 67, 0.05) 100%);
}

.progress-item {
  margin-bottom: 20px;
}

.progress-item:last-child {
  margin-bottom: 0;
}

.progress-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.progress-title {
  font-weight: 600;
  font-size: 14px;
  color: var(--text-primary);
}

.progress-stat {
  font-size: 13px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.progress-bar {
  height: 12px;
  background: var(--bg-secondary);
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border-color);
}

.progress-fill {
  height: 100%;
  transition: width 0.4s ease;
}

.raw-fill {
  background: linear-gradient(90deg, var(--color-success), var(--color-info));
}

.craftable-fill {
  background: linear-gradient(90deg, var(--color-warning), var(--color-success));
}

/* ============================================================================
   TABLE & DATA
   ============================================================================ */

@keyframes fling-down {
  0% {
    transform: translateY(-20px);
    opacity: 1;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes item-complete {
  0% {
    background-color: rgba(46, 160, 67, 0.2);
    transform: scale(1);
  }
  50% {
    background-color: rgba(46, 160, 67, 0.4);
    transform: scale(1.02);
  }
  100% {
    background-color: transparent;
    transform: scale(1);
  }
}

.animating-move {
  animation: fling-down 0.5s ease-out;
}

.animating-complete {
  animation: item-complete 0.6s ease-out;
}

#materialsTable {
  width: 100%;
  border-collapse: collapse;
  margin: 0;
}

#materialsTable thead {
  background: var(--bg-secondary);
  border-bottom: 2px solid var(--border-color);
}

#materialsTable th {
  padding: 12px;
  text-align: left;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

#materialsTable td {
  padding: 12px;
  border-bottom: 1px solid var(--border-color);
  vertical-align: middle;
}

#materialsTable tbody tr {
  transition: background-color 0.2s;
}

#materialsTable tbody tr:hover {
  background-color: rgba(88, 166, 255, 0.05);
}

#materialsTable th.count,
#materialsTable td.count {
  text-align: right;
  font-variant-numeric: tabular-nums;
  padding-right: 16px;
  position: relative;
  cursor: help;
}

/* Tooltip on hover */
#materialsTable td.count:hover::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  right: 0;
  background: var(--bg-secondary);
  color: var(--text-primary);
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  z-index: 10;
  margin-bottom: 8px;
  animation: tooltip-fade-in 0.2s ease-out;
}

/* Tooltip arrow */
#materialsTable td.count:hover::before {
  content: '';
  position: absolute;
  bottom: calc(100% - 6px);
  right: 8px;
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid var(--border-color);
  z-index: 10;
}

@keyframes tooltip-fade-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Raw Material Rows */
.raw-item {
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-primary);
  transition: color 0.2s;
}

.raw-item:hover {
  color: var(--color-info);
}

.raw-item.done {
  color: var(--color-success);
}

.row-arrow {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  font-size: 14px;
  user-select: none;
}

.row-icon {
  display: inline-flex;
  align-items: center;
  font-size: 18px;
}

/* Child/Craftable Rows */
.child-item {
  padding-left: 48px;
  font-style: italic;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 8px;
}

.child-item .row-icon {
  flex-shrink: 0;
  font-size: 16px;
}

.row-crafted .child-item,
.row-crafted .count {
  color: var(--text-muted);
  text-decoration: line-through;
  opacity: 0.6;
}

/* ============================================================================
   BUTTONS
   ============================================================================ */

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  white-space: nowrap;
  user-select: none;
}

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

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

/* Primary Button */
.btn-primary {
  background: var(--color-info);
  color: white;
  width: 100%;
  margin-top: 8px;
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: #4fa3ff;
  box-shadow: 0 0 12px rgba(88, 166, 255, 0.3);
}

/* Secondary Button */
.btn-secondary {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
}

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

/* Danger Button */
.btn-danger {
  background: var(--color-danger);
  color: white;
  border: 1px solid var(--color-danger);
}

.btn-danger:hover:not(:disabled) {
  background: #d1242f;
  box-shadow: 0 0 12px rgba(218, 54, 51, 0.3);
}

/* Item Action Buttons */
.btn-done {
  background: none;
  border: 2px solid var(--color-success);
  color: var(--color-success);
  padding: 6px 12px;
  min-width: 110px;
}

.btn-done:hover:not(:disabled) {
  background: rgba(46, 160, 67, 0.1);
}

.btn-done.active {
  background: rgba(46, 160, 67, 0.2);
}

.btn-undone {
  background: none;
  border: 2px solid var(--color-danger);
  color: var(--color-danger);
  padding: 6px 12px;
  min-width: 110px;
}

.btn-undone:hover:not(:disabled) {
  background: rgba(218, 54, 51, 0.1);
}

.btn-undone.active {
  background: rgba(218, 54, 51, 0.2);
}

/* Crafted Button */
.btn-crafted {
  border: 1px solid var(--color-warning);
  color: var(--color-warning);
  background: none;
  padding: 6px 12px;
  min-width: 110px;
  font-style: italic;
}

.btn-crafted:hover:not(:disabled) {
  background: rgba(249, 168, 37, 0.1);
  border-color: var(--color-warning);
}

.btn-crafted.active {
  border-color: var(--color-success);
  color: var(--color-success);
  background: rgba(46, 160, 67, 0.1);
  font-style: normal;
}

.btn-crafted.active::before {
  content: "✔ ";
}

/* ============================================================================
   RESPONSIVE
   ============================================================================ */

@media (max-width: 768px) {
  .container {
    padding: 16px;
  }

  .header {
    margin-bottom: 24px;
    padding-bottom: 16px;
  }

  .header h1 {
    font-size: 24px;
  }

  .card {
    padding: 16px;
    margin-bottom: 16px;
  }

  .section-row {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .child-item {
    padding-left: 40px;
  }

  .input-section textarea {
    min-height: 120px;
  }

  #materialsTable td,
  #materialsTable th {
    padding: 10px 8px;
    font-size: 13px;
  }

  .btn {
    padding: 6px 12px;
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 12px;
  }

  .header h1 {
    font-size: 20px;
  }

  .subtitle {
    font-size: 14px;
  }

  .card h2 {
    font-size: 16px;
  }

  .section-row {
    gap: 12px;
  }

  #materialsTable th.count,
  #materialsTable td.count {
    display: none;
  }

  #materialsTable th:last-child,
  #materialsTable td:last-child {
    text-align: left;
    padding-left: 8px;
  }

  .progress-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}
