:root {
  --bg: #faf8f5;
  --bg-alt: #f5f2ed;
  --panel: #fffefc;
  --card: rgba(255, 255, 255, 0.72);
  --border: #e5e0da;
  --text: #4a4540;
  --text-muted: #6d6761;
  --accent: #705746;
  --accent-hover: #5c4738;
  --accent-soft: #e8e0d8;
  --success: #6f8f5c;
  --danger: #a44d48;
  --radius: 14px;
  --shadow: 0 8px 32px rgba(74, 69, 64, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "LINE Seed JP", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Segoe UI", system-ui, -apple-system, sans-serif;
  overflow-x: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

/* Header */
.header {
  border-bottom: 1px solid var(--border);
  background: rgba(250, 248, 245, 0.9);
  backdrop-filter: blur(12px);
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px 8px;
}
.brand {
  display: inline-flex;
  align-items: baseline;
  gap: 10px;
  min-width: max-content;
  text-decoration: none;
}
.brand-name {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--text);
}
.brand-separator {
  width: 1px;
  height: 14px;
  background: var(--border);
}
.brand-tool {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
}
.nav-links {
  display: flex;
  align-items: center;
  gap: 16px;
  list-style: none;
  margin: 0;
  padding: 0;
}
.nav-links a {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-muted);
  text-decoration: none;
  transition: color .2s;
}
.nav-links a:hover { color: var(--text); }
.nav-links .site-link { color: var(--accent); }

.app-subtitle {
  margin: 0;
  padding: 0 24px 14px;
  font-size: 13px;
  color: var(--text-muted);
}

/* Body layout */
.app-body {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* Sidebar */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--card);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.dropzone {
  margin: 14px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 18px 10px;
  text-align: center;
  transition: border-color .15s, background .15s;
  cursor: pointer;
}
.dropzone.dragover {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.dropzone-inner p {
  margin: 6px 0 10px;
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.4;
}
.dropzone-icon {
  font-size: 22px;
  color: var(--accent);
  line-height: 1;
  margin-bottom: 4px;
}

.sidebar-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px 8px;
  font-size: 12px;
  color: var(--text-muted);
}
.file-count { font-weight: 600; }

.file-list {
  list-style: none;
  margin: 0;
  padding: 0 10px 14px;
  overflow-y: auto;
  flex: 1;
}

.file-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
  margin-bottom: 6px;
  position: relative;
}
.file-item:hover { background: var(--bg-alt); }
.file-item.selected {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.file-thumb-wrap {
  position: relative;
  width: 48px;
  height: 48px;
  flex-shrink: 0;
  border-radius: 6px;
  overflow: hidden;
  background: var(--bg-alt);
}
.file-thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.file-check {
  position: absolute;
  bottom: -3px;
  right: -3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--success);
  color: #fff;
  font-size: 11px;
  display: none;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--panel);
}
.file-item.edited .file-check { display: flex; }

.file-meta {
  flex: 1;
  min-width: 0;
}
.file-name {
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-status {
  font-size: 11px;
  color: var(--text-muted);
}
.file-item.edited .file-status { color: var(--success); }

.file-remove {
  flex-shrink: 0;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
}
.file-remove:hover { background: #f5e5e3; color: var(--danger); }

/* Editor */
.editor {
  flex: 1;
  display: flex;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
}
.editor-empty {
  margin: auto;
  color: var(--text-muted);
  font-size: 14px;
}

.editor-content {
  display: flex;
  gap: 24px;
  padding: 24px;
  width: 100%;
}

.editor-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.crop-stage {
  width: 100%;
  max-width: 560px;
  height: 460px;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    linear-gradient(45deg, #e9eaee 25%, transparent 25%),
    linear-gradient(-45deg, #e9eaee 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e9eaee 75%),
    linear-gradient(-45deg, transparent 75%, #e9eaee 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.crop-stage canvas {
  cursor: grab;
  touch-action: none;
  box-shadow: var(--shadow);
  border-radius: 4px;
}
.crop-stage canvas:active { cursor: grabbing; }

.crop-hint {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
}

.editor-controls {
  width: 280px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.control-group {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.control-group h2 {
  margin: 0 0 10px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .02em;
}

.aspect-buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.aspect-btn {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 999px;
  padding: 7px 4px;
  min-height: 40px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  color: var(--text-muted);
  touch-action: manipulation;
  transition: color .2s, border-color .2s, background .2s;
}
.aspect-btn:hover { color: var(--text); border-color: var(--accent); }
.aspect-btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--text);
  font-weight: 600;
}

.custom-aspect {
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}
.custom-aspect input {
  width: 56px;
  padding: 5px 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  background: var(--panel);
  color: var(--text);
}

.custom-aspect-hint {
  margin: 8px 0 0;
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.4;
}

input[type="range"] {
  width: 100%;
}

.filename-row {
  display: flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel);
}
.filename-row input {
  flex: 1;
  min-width: 0;
  border: none;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
  background: transparent;
  outline: none;
}
.filename-ext {
  padding: 8px 10px;
  font-size: 13px;
  color: var(--text-muted);
  background: var(--bg-alt);
  border-left: 1px solid var(--border);
}

#formatSelect {
  width: 100%;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
  background: var(--panel);
}
.quality-row {
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-muted);
}
.quality-row label { display: block; margin-bottom: 4px; }

/* Buttons */
.btn {
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  padding: 9px 16px;
  min-height: 42px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .15s;
  touch-action: manipulation;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled {
  opacity: .5;
  cursor: not-allowed;
}
.btn-block { width: 100%; margin-top: 8px; }
.btn-small { font-size: 12px; padding: 6px 12px; min-height: 36px; }
.btn-text {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 4px 6px;
  min-height: auto;
}
.btn-text:hover { color: var(--danger); }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); transform: translateY(-1px); }

/* Footer */
.app-footer {
  border-top: 1px solid var(--border);
  background: var(--card);
  padding: 12px 24px;
  padding-left: calc(24px + env(safe-area-inset-left));
  padding-right: calc(24px + env(safe-area-inset-right));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.footer-status {
  font-size: 13px;
  color: var(--text-muted);
}

/* Legal footer (site-wide policy links) */
.legal-footer {
  padding: 20px 24px;
  padding-bottom: calc(20px + env(safe-area-inset-bottom));
  padding-left: calc(24px + env(safe-area-inset-left));
  padding-right: calc(24px + env(safe-area-inset-right));
  border-top: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  font-size: 12.5px;
  text-align: center;
}
.legal-note {
  max-width: 640px;
  margin: 0 auto 14px;
  line-height: 1.6;
}
.footer-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-bottom: 12px;
}
.footer-link {
  display: inline-flex;
  align-items: center;
  padding: 5px 12px;
  font-size: 12px;
  color: var(--text-muted);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--card);
  transition: border-color .2s, color .2s;
}
.footer-link:hover { border-color: var(--accent); color: var(--text); }
.copyright { margin: 0; }

/* Whole-window drag overlay */
.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(112, 87, 70, 0.12);
  backdrop-filter: blur(1px);
  pointer-events: none;
}
.drop-overlay.active { display: flex; }
.drop-overlay-inner {
  background: var(--panel);
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  padding: 40px 64px;
  text-align: center;
  box-shadow: var(--shadow);
}
.drop-overlay-inner p {
  margin: 8px 0 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
}
.drop-overlay .dropzone-icon { font-size: 32px; }

/* Toast notifications */
.toast-stack {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-end;
}
.toast {
  background: #3d332b;
  color: #fff;
  font-size: 13px;
  padding: 10px 16px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .18s ease, transform .18s ease;
  max-width: 320px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.toast-success { background: var(--success); }

@media (max-width: 860px) {
  .app-body { flex-direction: column; }
  .sidebar { width: 100%; max-height: 260px; border-right: none; border-bottom: 1px solid var(--border); }
  .editor-content { flex-direction: column; }
  .editor-controls { width: 100%; }
}

/* Phones: sidebar becomes a horizontally scrolling thumbnail strip so the
   crop editor keeps most of the vertical space. */
@media (max-width: 600px) {
  .nav { padding: 12px 16px 6px; gap: 10px; }
  .brand-name { font-size: 16px; }
  .brand-tool { display: none; }
  .brand-separator { display: none; }
  .nav-links { gap: 10px; }
  .nav-links a { font-size: 11.5px; }
  .app-subtitle { padding: 0 16px 10px; font-size: 12px; }

  .sidebar { max-height: none; }

  .dropzone { margin: 10px; padding: 12px 10px; }
  .dropzone-inner p { font-size: 12px; margin: 4px 0 8px; }

  .sidebar-toolbar { padding: 0 10px 8px; }

  .file-list {
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: visible;
    gap: 8px;
    padding: 0 10px 14px;
    -webkit-overflow-scrolling: touch;
  }
  .file-item {
    flex: 0 0 auto;
    width: 84px;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 4px;
    margin-bottom: 0;
    padding: 8px 6px 6px;
  }
  .file-thumb-wrap { width: 60px; height: 60px; }
  .file-meta { width: 100%; }
  .file-name { font-size: 11px; text-align: center; }
  .file-status { display: none; }
  .file-remove {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 22px;
    height: 22px;
    background: rgba(255, 255, 255, .9);
    box-shadow: var(--shadow);
  }

  .editor-content { padding: 12px; gap: 14px; }
  .crop-stage { height: min(65vw, 380px); }
  .editor-controls { gap: 12px; }
  .control-group { padding: 10px 12px; }
  .aspect-buttons { gap: 8px; }

  .app-footer { flex-direction: column; align-items: stretch; gap: 8px; padding: 10px 14px; }
  .footer-status { text-align: center; }
  #downloadAllBtn { width: 100%; }

  .legal-footer { padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

  .toast-stack { left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom)); align-items: stretch; }
  .toast { max-width: none; text-align: center; }

  .drop-overlay-inner { padding: 28px 32px; }
}
