:root {
  --bg: #0b0f14;
  --bg-2: #10161d;
  --panel: #131b24;
  --panel-2: #18222d;
  --line: #22303d;
  --line-soft: #1a2531;
  --text: #e8eef5;
  --muted: #8fa0b3;
  --dim: #64758a;
  --accent: #4fc3a1;
  --accent-2: #2f9e82;
  --accent-ink: #04211a;
  --danger: #ef6f6f;
  --warn: #e9b45c;
  --radius: 12px;
  --shadow: 0 18px 50px rgba(0, 0, 0, .45);
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: radial-gradient(1200px 700px at 15% -10%, #16232e 0%, transparent 60%), var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI Variable", "Segoe UI", "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------------ layout */
#app {
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

#sidebar {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 12px;
  background: linear-gradient(180deg, rgba(19, 27, 36, .95), rgba(11, 15, 20, .95));
  border-right: 1px solid var(--line-soft);
  overflow-y: auto;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px 14px; }
.brand-mark {
  width: 34px; height: 34px; flex: none;
  display: grid; place-items: center;
  border-radius: 10px;
  background: linear-gradient(150deg, var(--accent), #2b7f9e);
  color: var(--accent-ink);
  font-weight: 800; font-size: 17px;
  box-shadow: 0 6px 18px rgba(79, 195, 161, .25);
}
.brand-mark.big { width: 46px; height: 46px; font-size: 22px; border-radius: 13px; margin: 0 auto 6px; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong { letter-spacing: .12em; font-size: 13px; }
.brand-text span { color: var(--dim); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand .icon-btn { margin-left: auto; }

.side-heading {
  margin: 12px 8px 4px;
  font-size: 11.5px;
  letter-spacing: .06em;
  color: var(--dim);
}

.root-list, .filter-list { display: flex; flex-direction: column; gap: 2px; }
.root-list button, .filter-list button {
  display: flex; align-items: center; gap: 9px;
  width: 100%;
  padding: 8px 9px;
  border: 0; border-radius: 9px;
  background: transparent;
  color: var(--muted);
  text-align: left;
  cursor: pointer;
}
.root-list button:hover, .filter-list button:hover { background: var(--panel-2); color: var(--text); }
.root-list button.active, .filter-list button.active {
  background: linear-gradient(90deg, rgba(79, 195, 161, .16), rgba(79, 195, 161, .04));
  color: var(--text);
  box-shadow: inset 0 0 0 1px rgba(79, 195, 161, .22);
}
.root-list .rdot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; box-shadow: 0 0 10px rgba(79, 195, 161, .6); }
.root-list .rmeta { margin-left: auto; font-size: 11px; color: var(--dim); font-family: var(--mono); }
.root-list .rname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.side-foot { margin-top: auto; padding: 10px 6px 2px; }
.storage-row { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--dim); margin-bottom: 6px; }
.bar { height: 5px; border-radius: 99px; background: #1d2833; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), #7ad0ff); transition: width .3s ease; }
.hint { margin-top: 10px; font-size: 11px; color: var(--dim); }
.side-link {
  width: 100%; margin-top: 10px; padding: 7px 9px;
  border: 1px solid var(--line-soft); border-radius: 8px;
  background: transparent; color: var(--muted);
  font-size: 12px; text-align: left; cursor: pointer;
}
.side-link:hover { background: var(--panel-2); color: var(--text); }

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

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line-soft);
  background: rgba(11, 15, 20, .72);
  backdrop-filter: blur(10px);
  /* backdrop-filter makes this a stacking context; without an explicit
     z-index it painted below the sticky table headers (z-index 2), so the
     upload menu hover/click fell through to the "修改時間" cells. */
  position: relative;
  z-index: 3;
}
.crumbs { display: flex; align-items: center; gap: 2px; min-width: 0; overflow: hidden; flex: 1 1 auto; }
.crumbs button {
  border: 0; background: transparent; color: var(--muted);
  padding: 6px 8px; border-radius: 8px; cursor: pointer;
  white-space: nowrap; max-width: 220px; overflow: hidden; text-overflow: ellipsis;
}
.crumbs button:hover { background: var(--panel-2); color: var(--text); }
.crumbs button.current { color: var(--text); font-weight: 600; }
.crumbs .sep { color: var(--dim); font-size: 12px; padding: 0 1px; }

.search { position: relative; flex: 0 1 300px; }
.search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--dim); width: 16px; height: 16px; }
.search input {
  width: 100%;
  padding: 8px 10px 8px 32px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--panel);
  outline: none;
}
.search input:focus { border-color: rgba(79, 195, 161, .6); box-shadow: 0 0 0 3px rgba(79, 195, 161, .12); }

.actions { display: flex; gap: 8px; position: relative; }
.upload-menu {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 20;
  min-width: 168px; padding: 5px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow);
}
.upload-menu button {
  display: block; width: 100%; padding: 8px 10px;
  border: 0; border-radius: 7px; background: transparent; color: var(--text);
  text-align: left; font-size: 13px; cursor: pointer;
}
.upload-menu button:hover { background: var(--panel-2); }
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-2); border-color: #2b3b4b; }
.btn.primary { background: linear-gradient(180deg, var(--accent), var(--accent-2)); border-color: transparent; color: var(--accent-ink); font-weight: 650; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: rgba(239, 111, 111, .35); }
.btn.danger:hover { background: rgba(239, 111, 111, .1); }
.btn.wide { width: 100%; justify-content: center; padding: 10px; }
.icon-btn {
  display: grid; place-items: center;
  width: 32px; height: 32px;
  border-radius: 8px; border: 0; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

.toolbar { display: flex; align-items: center; gap: 14px; padding: 10px 18px; border-bottom: 1px solid var(--line-soft); }
.seg { display: inline-flex; padding: 2px; border-radius: 9px; background: var(--panel); border: 1px solid var(--line-soft); }
.seg button { display: grid; place-items: center; width: 30px; height: 26px; border: 0; border-radius: 7px; background: transparent; color: var(--dim); cursor: pointer; }
.seg button.active { background: var(--panel-2); color: var(--text); }
.check { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: 12.5px; cursor: pointer; user-select: none; }
.check input, .files input[type=checkbox] { accent-color: var(--accent); }
.spacer { flex: 1; }
.selection { display: flex; align-items: center; gap: 10px; font-size: 12.5px; color: var(--muted); }
.selection .btn { padding: 5px 9px; font-size: 12.5px; }

.content { flex: 1; min-height: 0; overflow: auto; position: relative; }

/* ------------------------------------------------------------------- table */
.table-wrap { padding: 4px 10px 20px; }
table.files { width: 100%; border-collapse: collapse; }
table.files th {
  position: sticky; top: 0; z-index: 2;
  background: var(--bg);
  font-size: 12px; font-weight: 600; letter-spacing: .04em;
  color: var(--dim);
  text-align: left;
  padding: 10px 10px;
  border-bottom: 1px solid var(--line-soft);
}
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sorted { color: var(--accent); }
th.sorted::after { content: " ▾"; font-size: 10px; }
th.sorted.asc::after { content: " ▴"; }
table.files td { padding: 6px 10px; border-bottom: 1px solid var(--line-soft); }
table.files tbody tr { cursor: default; }
table.files tbody tr:hover { background: rgba(255, 255, 255, .025); }
table.files tbody tr.selected { background: rgba(79, 195, 161, .09); }
.col-check { width: 36px; }
.col-size { width: 110px; }
.col-date { width: 170px; }
.col-act { width: 44px; }
.size, .date { color: var(--muted); font-family: var(--mono); font-size: 12px; white-space: nowrap; }
.size.dir { color: var(--dim); }

.name-cell { display: flex; align-items: center; gap: 10px; min-width: 0; }
.ficon {
  width: 30px; height: 30px; flex: none;
  display: grid; place-items: center;
  border-radius: 8px;
  font-size: 9.5px; font-weight: 800; letter-spacing: .02em;
  font-family: var(--mono);
  background: var(--panel-2);
  color: var(--muted);
  text-transform: uppercase;
}
.ficon.dir { background: rgba(233, 180, 92, .12); color: var(--warn); }
.ficon.img { background: rgba(122, 208, 255, .12); color: #7ad0ff; }
.ficon.vid { background: rgba(186, 132, 255, .14); color: #ba84ff; }
.ficon.aud { background: rgba(79, 195, 161, .14); color: var(--accent); }
.ficon.doc { background: rgba(143, 160, 179, .12); color: #b7c6d6; }
.ficon.mod { background: rgba(239, 111, 111, .12); color: #f08a8a; }
.fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fname.dir { color: #f2e6cd; }
.row-actions { display: flex; gap: 2px; opacity: 0; }
tr:hover .row-actions, tr.selected .row-actions { opacity: 1; }
.row-actions .icon-btn { width: 26px; height: 26px; }
.row-actions svg { width: 15px; height: 15px; }

/* -------------------------------------------------------------------- grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(158px, 1fr)); gap: 12px; padding: 14px 18px 30px; }
.card {
  position: relative;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--panel);
  overflow: hidden;
  cursor: default;
  transition: transform .12s ease, border-color .12s ease;
}
.card:hover { transform: translateY(-2px); border-color: #2c3d4e; }
.card.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.card .thumb { height: 106px; display: grid; place-items: center; background: #0e141b; overflow: hidden; }
.card .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .thumb .ficon { width: 46px; height: 46px; font-size: 13px; border-radius: 12px; }
.card .meta { padding: 8px 10px 10px; }
.card .meta .fname { font-size: 12.5px; }
.card .meta .sub { color: var(--dim); font-size: 11px; font-family: var(--mono); margin-top: 2px; }
.card .card-check { position: absolute; top: 8px; left: 8px; opacity: 0; }
.card:hover .card-check, .card.selected .card-check { opacity: 1; }

/* ------------------------------------------------------------------ states */
.empty { display: grid; place-items: center; gap: 4px; padding: 80px 20px; color: var(--dim); text-align: center; }
.empty-icon { font-size: 40px; color: #2c3d4e; }
.empty p { margin: 0 0 14px; }

.statusbar {
  display: flex; justify-content: space-between;
  padding: 7px 18px;
  border-top: 1px solid var(--line-soft);
  background: var(--bg-2);
  font-size: 11.5px; color: var(--dim);
  font-family: var(--mono);
}

/* ------------------------------------------------------------------- login */
.login-screen {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 600px at 50% 0%, rgba(79, 195, 161, .12), transparent 60%),
    var(--bg);
}
.login-card {
  width: min(360px, 100%);
  display: flex; flex-direction: column; gap: 12px;
  padding: 30px 26px 22px;
  background: linear-gradient(180deg, var(--panel), #0f171f);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 4px 0 0; font-size: 20px; text-align: center; letter-spacing: .02em; }
.login-card .sub { margin: 0 0 8px; text-align: center; color: var(--muted); font-size: 12.5px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
.login-card input {
  padding: 10px 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: #0c1218;
  outline: none;
}
.login-card input:focus { border-color: rgba(79, 195, 161, .65); box-shadow: 0 0 0 3px rgba(79, 195, 161, .12); }
.error { color: var(--danger); font-size: 12.5px; margin: 0; text-align: center; }
.fineprint { color: var(--dim); font-size: 11px; text-align: center; margin: 2px 0 0; }

/* ------------------------------------------------------------------ upload */
.drop-overlay {
  position: fixed; inset: 0; z-index: 30;
  display: grid; place-items: center;
  background: rgba(4, 10, 14, .78);
  border: 2px dashed rgba(79, 195, 161, .7);
  color: var(--text);
  font-size: 17px;
  pointer-events: none;
}
.upload-panel {
  position: fixed; right: 18px; bottom: 46px; z-index: 35;
  width: min(360px, calc(100vw - 36px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.upload-head { display: flex; align-items: center; justify-content: space-between; padding: 9px 10px 9px 14px; border-bottom: 1px solid var(--line-soft); font-size: 12.5px; color: var(--muted); }
.upload-agg { padding: 8px 12px 2px; }
.upload-items { max-height: 220px; overflow: auto; padding: 6px 12px 12px; }
.u-item { padding: 7px 0; font-size: 12px; }
.u-item .u-name { display: flex; justify-content: space-between; gap: 10px; color: var(--muted); margin-bottom: 4px; }
.u-item .u-name span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.u-item .bar i { background: linear-gradient(90deg, var(--accent), #7ad0ff); }
.u-item.done .bar i { background: var(--accent); }
.u-item.failed .bar i { background: var(--danger); }

/* ------------------------------------------------------------------- modal */
.modal { position: fixed; inset: 0; z-index: 45; display: grid; place-items: center; padding: 20px; background: rgba(3, 7, 10, .72); backdrop-filter: blur(3px); }
.modal-box {
  position: relative;
  width: min(920px, 100%);
  max-height: calc(100vh - 40px);
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 18px;
}
.modal-close { position: absolute; top: 10px; right: 10px; }
.modal h2 { margin: 0 0 12px; font-size: 15px; padding-right: 30px; overflow-wrap: anywhere; }
.modal .preview { display: grid; place-items: center; background: #070b0f; border-radius: 10px; overflow: hidden; }
.modal .preview img, .modal .preview video { max-width: 100%; max-height: 68vh; display: block; }
.modal .preview audio { width: 100%; margin: 30px 0; }
.modal .preview iframe { width: 100%; height: 68vh; border: 0; background: #fff; }
.modal pre { margin: 0; max-height: 66vh; overflow: auto; background: #070b0f; border-radius: 10px; padding: 14px; font: 12.5px/1.55 var(--mono); color: #cfe1ef; white-space: pre-wrap; word-break: break-word; }
.modal .meta-line { margin-top: 10px; color: var(--dim); font-size: 11.5px; font-family: var(--mono); display: flex; gap: 16px; flex-wrap: wrap; }
.modal .form { display: flex; flex-direction: column; gap: 10px; max-width: 420px; }
.modal .form input { padding: 9px 11px; border-radius: 9px; border: 1px solid var(--line); background: #0c1218; outline: none; }
.modal .form input:focus { border-color: rgba(79, 195, 161, .6); }
.modal .form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
.modal .search-hit { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px; cursor: pointer; }
.modal .search-hit:hover { background: var(--panel-2); }
.modal .search-hit .p { color: var(--dim); font-size: 11.5px; font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --------------------------------------------------------------- ctx / toast */
.ctx {
  position: fixed; z-index: 50;
  min-width: 190px;
  padding: 5px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.ctx button {
  display: flex; align-items: center; gap: 9px;
  width: 100%; padding: 8px 10px;
  border: 0; border-radius: 7px;
  background: transparent; color: var(--text);
  text-align: left; cursor: pointer;
  font-size: 13px;
}
.ctx button:hover { background: var(--panel-2); }
.ctx button.danger { color: var(--danger); }
.ctx hr { border: 0; border-top: 1px solid var(--line-soft); margin: 4px 2px; }

.toasts { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  padding: 9px 16px;
  border-radius: 99px;
  background: #1b2733;
  border: 1px solid #2c3d4e;
  color: var(--text);
  font-size: 12.5px;
  box-shadow: var(--shadow);
  animation: toast-in .18s ease;
}
.toast.bad { border-color: rgba(239, 111, 111, .5); color: #ffc9c9; }
.toast.good { border-color: rgba(79, 195, 161, .5); color: #b9f2e2; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ------------------------------------------------------------- responsive */
.only-mobile { display: none; }
@media (max-width: 900px) {
  #app { grid-template-columns: minmax(0, 1fr); }
  #sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 25;
    width: 262px;
    transform: translateX(-102%);
    transition: transform .2s ease;
    box-shadow: var(--shadow);
  }
  #sidebar.open { transform: none; }
  .only-mobile { display: grid; }
  .search { flex: 0 1 170px; }
  .actions .btn span { display: none; }
  .actions .btn { padding: 8px 10px; }
  .crumbs button { max-width: 120px; }
  .col-date { display: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); padding: 12px; }
}
@media (max-width: 560px) {
  .search { flex: 1 1 auto; }
  .statusbar { font-size: 10.5px; }
  .col-size { width: 74px; }
  #newFolderBtn { display: none; }
}
